天天看點

解決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,筆記本和移動裝置都能加載網頁。