zoukankan      html  css  js  c++  java
  • 解决nginx+uWSGI部署Django时遇到的static文件404的问题

    昨天是利用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,笔记本和移动设备都能加载网页。

  • 相关阅读:
    《你不知道的javascript》读书笔记2
    你不知道的console调试
    《你不知道的javascript》读书笔记1
    使用js做LeetCode
    用装饰器来进行登录验证
    python 解压序列
    pycharm 的live_template的使用
    faker 库的使用
    Python常用内置类和常用内置函数汇总
    迭代器 ,生成器(生成器函数/生成器表达式)
  • 原文地址:https://www.cnblogs.com/slontia/p/7747902.html
Copyright © 2011-2022 走看看