昨天是利用Django自带的runserver部署的服务器,但是由于runserver比较不稳定,因此决定采用uWSGI+nginx进行部署。

昨天已经安装好了uwsgi和nginx,使用该指令打开8000访问端口:

uwsgi --http :8000 --chdir /home/icourse/iCourse --module iCourse.wsgi

然后利用笔记本和平板电脑分别访问,发现页面一片空白。

服务器后台的错误信息,:

[pid: 31549|app: 0|req: 1/1] 10.137.174.21 () {70 vars in 1019 bytes} [Sat Oct 28 11:25:09 2017] GET / => generated 442 bytes in 41 msecs (HTTP/1.1 200) 3 headers in 109 bytes (1 switches on core 0)
Not Found: /static/css/app.da991ce51b08fcdf1dfde7fb00a9d017.css
[pid: 31549|app: 0|req: 2/2] 10.137.174.21 () {70 vars in 1161 bytes} [Sat Oct 28 11:25:09 2017] GET /static/css/app.da991ce51b08fcdf1dfde7fb00a9d017.css => generated 2200 bytes in 12 msecs (HTTP/1.1 404) 3 headers in 102 bytes (1 switches on core 0)
Not Found: /static/js/vendor.77bc9ca30309ef3aa829.js
[pid: 31550|app: 0|req: 1/3] 10.137.174.21 () {70 vars in 1102 bytes} [Sat Oct 28 11:25:09 2017] GET /static/js/vendor.77bc9ca30309ef3aa829.js => generated 2167 bytes in 28 msecs (HTTP/1.1 404) 3 headers in 102 bytes (1 switches on core 0)
Not Found: /static/js/app.d206dc1dbbd970ddb09c.js
Not Found: /static/js/manifest.34417dabbe075f84e501.js
[pid: 31547|app: 0|req: 1/4] 10.137.174.21 () {70 vars in 1090 bytes} [Sat Oct 28 11:25:09 2017] GET /static/js/app.d206dc1dbbd970ddb09c.js => generated 2158 bytes in 49 msecs (HTTP/1.1 404) 3 headers in 102 bytes (1 switches on core 0)
[pid: 31548|app: 0|req: 1/5] 10.137.174.21 () {70 vars in 1110 bytes} [Sat Oct 28 11:25:09 2017] GET /static/js/manifest.34417dabbe075f84e501.js => generated 2173 bytes in 44 msecs (HTTP/1.1 404) 3 headers in 102 bytes (1 switches on core 0)
Not Found: /static/js/vendor.77bc9ca30309ef3aa829.js
[pid: 31550|app: 0|req: 2/6] 10.137.174.21 () {70 vars in 1102 bytes} [Sat Oct 28 11:25:09 2017] GET /static/js/vendor.77bc9ca30309ef3aa829.js => generated 2167 bytes in 8 msecs (HTTP/1.1 404) 3 headers in 102 bytes (1 switches on core 0)
Not Found: /static/js/app.d206dc1dbbd970ddb09c.js
[pid: 31550|app: 0|req: 3/7] 10.137.174.21 () {70 vars in 1090 bytes} [Sat Oct 28 11:25:09 2017] GET /static/js/app.d206dc1dbbd970ddb09c.js => generated 2158 bytes in 8 msecs (HTTP/1.1 404) 3 headers in 102 bytes (1 switches on core 0)

之前使用runserver的时候一切正常,换成nginx就出了问题。而且更奇怪的是凡是在runserver时访问过网站的设备,都能在运行uwsgi时访问网页,而其它设备就不行(这个很玄,原因暂时还不清楚)。

经过了一下午的查阅资料,最终解决问题。

首先我在/home/icourse/下建了两个文件,一个是uwsgi8000.ini,一个是nginx.conf,按照网上的代码照猫画虎进行了配置,并将nginx.conf软连接到/etc/nginx/sites-enabled/下:

sudo ln -s ~/home/icourse/nginx.conf /etc/nginx/sites-enabled/

然后运行如下指令:

uwsgi --ini ./uwsgi8000.ini

这就是使用ini的好处,比第一条指令简单多了。

然后发现浏览器提示没有发送信息,后台也没有一点提示。

后来又查找了很多资料,搞清楚了socket和http的概念,个人理解是http(设置为:8000)是提供用户访问的,socket(设置为127.0.0.1:8001)是nginx和uwsgi进行信息交流用的,之前一直用的是socket,没有定义用户访问的接口,所以导致无法连接。

经过了一番修改,nginx.conf内容如下(项目名为iCourse):

upstream django {
server 127.0.0.1:8001; # 和ini文件中的socket保持一致
} server {
listen 8000; # 访问接口
server_name origin_icourse; location / {
include uwsgi_params;
uwsgi_pass django;
include /etc/nginx/uwsgi_params;
uwsgi_param UWSGI_SCRIPT iCourse.wsgi;
uwsgi_param UWSGI_CHDIR /iCourse;
index index.html index.htm;
client_max_body_size 35m;
}
}

uwsgi8000.ini内容如下:

[uwsgi]
socket = 127.0.0.1:8001 # 和conf文件中的server保持一致
chdir = /home/icourse/iCourse # 项目位置
wsgi-file = iCourse/wsgi.py # wsgi.py位置(相对chdir)
master = true
processes = 4
#threads = 2
#module = iCourse.wsgi
vacuum = true # 清除文件
buffer-size = 30000

在此运行uwsgi,发现又回到了一开始的状态,即404错误。折腾了一下午仿佛又回到了原点。

然后又经过一番搜索,发现conf文件中缺少了对static路径的定义,于是在server语句块下又添加了如下代码:

location /static {
alias /home/icourse/iCourse/frontend/dist/static;
}

运行uwsgi,笔记本和移动设备都能加载网页。

解决nginx+uWSGI部署Django时遇到的static文件404的问题的更多相关文章

  1. 填坑!!!virtualenv 中 nginx + uwsgi 部署 django

    一.为什么会有这篇文章 第一次接触 uwsgi 和 nginx ,这个环境搭建,踩了太多坑,现在记录下来,让后来者少走弯路. 本来在 Ubuntu14.04 上 搭建好了环境,然后到 centos7. ...

  2. nginx + uwsgi 部署 Django+Vue项目

    nginx + uwsgi 部署 Django+Vue项目 windows 本地 DNS 解析 文件路径 C:\Windows\System32\drivers\etc 单机本地测试运行方式,调用dj ...

  3. Python3.6+nginx+uwsgi部署Django程序到阿里云Ubuntu16.04系统

    Python3.6+nginx+uwsgi部署Django程序到阿里云Ubuntu16.04系统 这个是写好的Django程序在本地机运行的情况,一个查询接口. 准备工作 1.首先购买一台阿里云的EC ...

  4. nginx + uwsgi 部署django项目

    因项目需求,需要部署django项目,这里是基础的nginx配合uwsgi部署django,后续会采用docker部署的方式 环境: centos7 python3.5.4 django2.1.4 u ...

  5. 生产环境使用Nginx+uwsgi部署Django

    在本地运行django应用相对来说还是挺方便的,使用自带的runserver启动即可.如果在生产环境部署django,就要多考虑一些问题了.比如静态文件处理,安全,效率等等 在网上找到了不错的部署的教 ...

  6. Nginx + uWSGI 部署Django 项目,并实现负载均衡

    一.uWSGI服务器 uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换. 要注意 WSGI ...

  7. nginx+uwsgi部署Django项目到Ubuntu服务器全过程,以及那些坑!!!

    前言:自己在windows上用PyCharm编写的Django项目,编写完后在windows上运行一点问题都没有,但是部署到服务器上时却Bug百出.百度,CSDN,sf,各种搜索寻求解决方案在历时3天 ...

  8. CENTOS7 使用 Nginx + Uwsgi 部署 Django 项目

    写在前面的话 最近总是见到有新学 Django 的朋友在部署自己的项目到 Linux 上面的时候运行不起来,所以就动手写了这篇博客. 对于不会搭建 Python 3 环境的朋友可以参考前面的博客[CE ...

  9. 使用Nginx+uWSGI部署Django项目

    1.linux安装python3环境 参考链接:https://www.cnblogs.com/zzqit/p/10087680.html 2.安装uwsgi pip3 install uwsgi l ...

随机推荐

  1. 一些漂亮的js库

    http://tympanus.net/Development/ModalWindowEffects/ http://js1k.com/2013-spring/demos http://dimseme ...

  2. dos下edit编辑器的快捷命令一览

    Home Move cursor to the beginning of the line currently on. End Move cursor to the end of the line c ...

  3. log4j.properties配置文件详解

    Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以创建出Log4J的运行环境 ...

  4. CF GYM 101196 G That’s One Hanoi-ed Teacher

    That’s One Hanoi-ed Teacher 链接 题意: 询问一个汉诺塔的状态是否是最优的状态,如果是,询问还有多少步到最终状态. 分析: 考虑汉诺塔是怎么操作的,首先是考虑F(i)是有i ...

  5. 【BZOJ3555】企鹅QQ

    蛤希. 用map会T. 只需要枚举删掉哪个字符,然后算出每个的hash值,sort一遍就行了. 用map会T!!! // It is made by XZZ #include<cstdio> ...

  6. thymeleaf多条件判断

    解决办法:将逻辑关系全部写到大括号里面 <div th:if="${task.getStatusStr() !='已延期' ||task.getStatusStr()!='已完成'}& ...

  7. How to: Display a Non-Persistent Object's List View from the Navigation

    This example demonstrates how to display a non-persistent object's List View when a navigation item ...

  8. APP性能测试中的几个重要概念

    转载一篇文章,关于app性能测试的几个概念,对于想要接触app测试的朋友或许有些帮助. 我们在使用各种 App 的时候基本会关注到:这款软件挺耗流量的?运行起来设备掉电有点快嘛?切换页面的时候还会有卡 ...

  9. Makefile详解

    原文链接:https://blog.csdn.net/qq_38646470/article/details/79917494 专栏链接:https://blog.csdn.net/column/de ...

  10. 解决Eclipse Install New Software太慢的问题

    Eclipse -> Help -> Install New Software... 在出现的窗口点击Manage管理Available Software Sites 将所有URL中的&q ...