Nginx配置反向代理uwsgi
自django專案做完已有幾天,這幾天竟然都卡在了nginx配置上,在網上也搜了各種教程,但是就是無法訪問,我的內心是崩潰的。好在今天配置完了,發個部落格記錄一下配置檔案及過程,各種安裝過程就不說了,直接上配置檔案,正所謂:你離成功只差一個配置檔案!!!
1.各種路徑
專案名為:project
專案根目錄:/root/project
專案檔案結構:
project/
├── manage.py
└── project
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
uwsgi配置檔案路徑:/root/script/uwsgi.ini
nginx根目錄:
/etc/nginx
nginx預設配置檔案:
/etc/nginx/nginx.conf
專案自定義配置檔案:
/etc/nginx/conf.d/project.conf
2.配置uwsgi
2.1 uwsgi配置
# uwsig使用配置檔案啟動 [uwsgi] # 專案目錄 chdir=/root/project # 指定專案的application module=project.wsgi:application # 指定sock的檔案路徑 socket=/root/ctguinfowork/script/uwsgi.sock # 程序個數 workers=5 pidfile=/root/script/uwsgi.pid # 指定IP埠 http= :8080 # 指定靜態檔案,這個專案中沒有,如果你的專案中有靜態檔案的話按這個目錄結構配置 static-map=/static=/root/project/static # 啟動uwsgi的使用者名稱和使用者組 uid=root gid=root # 啟用主程序 master=true # 自動移除unix Socket和pid檔案當服務停止的時候 vacuum=true # 序列化接受的內容,如果可能的話 thunder-lock=true # 啟用執行緒 enable-threads=true # 設定自中斷時間 harakiri=30 # 設定緩衝 post-buffering=4096 # 設定日誌目錄 daemonize=/root/script/uwsgi.log
2.2啟動uwsgi
啟動uwsgi就比較簡單(如果沒有報錯的話):uwsgi --ini /root/script/uwsgi.ini
啟動成功後理論上來說就可以在瀏覽器欄輸入ip:port
來訪問專案了,port為uwsgi中配置的埠
3.配置Nginx
3.1 自定義配置
upstream project{ server 47.100.118.99:8080; } server { listen 80; #監聽埠 server_name 47.100.118.99; #訪問地址,這裡比較坑,填什麼就對映什麼,如果你填localhost、127.0.0.1之類的,就意味著你只能在本機瀏覽器上訪問,因為別人在自己電腦輸入127.0.0.1就不是你了 access_log /var/log/nginx/project.access.log main; #普通日誌 error_log /var/log/nginx/project.error.log; #錯誤日誌 #root html; #index index.html index.htm index.php; location / { proxy_pass http://project; #這裡http後等於第一行配置的名字 #Proxy Settings proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_max_temp_file_size 0; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } }
3.2 預設配置檔案
預設配置檔案基本上不用配置,保證其http{}裡面包含上面的自定義檔案就好了。
user root; #這裡改成root,因為預設nginx使用者可能沒有許可權訪問你的靜態檔案
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf; #保證配置檔案含有我們的自定義配置,其它路徑也可以
}
3.3 啟動nginx
啟動也比較簡單:/etc/nginx/nginx
直接執行這個nginx檔案就好了,有環境變數的可以直接執行
如果已經啟動的話,修改配置檔案後要重新載入配置:nginx -s reload
3.4 訪問專案
現在在瀏覽器輸入你的nginx配置就可以直接訪問uwsgi啟動了專案了(儘管我還沒想通為什麼uwsgi就可以訪問我非要用nginx),對映關係就是:nginxip:nginxport == uwsgiip:uwsgiport
4. 總結
這麼個簡單的配置竟然要這麼久,認真總結如下:
1.急於求成。尚不瞭解nginx、uwsgi等配置檔案含義的情況下直接使用,以致於一個小小的路徑引用錯誤就讓自己困惑很久
2.沒看官方文件的習慣。其實nginx官方文件(儘管它是英文版的)有很多配置方式和示例程式碼,完全不像百度搜到的部落格那樣繁瑣。而且一遇到問題就百度,可能將問題越加複雜化
3.可能是每天沒睡好變笨了