對於gunicorn+nginx的配置,理解他們之間的關系很重要,以及最后如何確認配置結果是正確的也很重要
nginx 配置文件:
修改這個配置文件有3個用處:
假設服務器本身的Ip是A稱為ip-A,而我用gunicorn啟動flask時寫的ip是127.0.0.1,用ip-B表示
1.當我在瀏覽器輸入http://ip-A:端口(nginx的端口) 時,nginx會把訪問地址指向http://ip-B:端口(gunicorn啟動的端口)
所以我們頁面看到的內容實際是gunicorn啟的flask的根頁面,
即視圖函數中app.route('/')裝飾器所裝飾的函數所返回頁面的內容
那么這種映身關系在nginx.conf配置文件中如何配置呢?
主要就是對proxy_pass進行配置
參數說明:
- listen是所需要監聽的端口
- server_name是需要綁定的域名,暫時沒有域名時,請使用ip
- location / 是當訪問到根下的時候,將所有請求轉發到127.0.0.1:8000,本文使轉發到gunicorn啟動的django應用上,中間配置的是需要轉發的內容,基本上述內容可以滿足大多需求,如需特殊需求請自行查看nginx官方文檔
- location /static/ 配置了靜態文件所在的路徑,靜態文件由nginx處理,動態轉發到django,如不配置會出現站點引用的所有js css都找不到
完整的配置文件在這里作個備份:
1 # For more information on configuration, see: 2 # * Official English Documentation: http://nginx.org/en/docs/ 3 # * Official Russian Documentation: http://nginx.org/ru/docs/ 4 5 user nginx; 6 worker_processes auto; 7 error_log /var/log/nginx/error.log; 8 pid /run/nginx.pid; 9 10 # Load dynamic modules. See /usr/share/nginx/README.dynamic. 11 include /usr/share/nginx/modules/*.conf; 12 13 events { 14 worker_connections 1024; 15 } 16 17 http { 18 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 19 '$status $body_bytes_sent "$http_referer" ' 20 '"$http_user_agent" "$http_x_forwarded_for"'; 21 22 access_log /var/log/nginx/access.log main; 23 24 sendfile on; 25 tcp_nopush on; 26 tcp_nodelay on; 27 keepalive_timeout 65; 28 types_hash_max_size 2048; 29 30 include /etc/nginx/mime.types; 31 default_type application/octet-stream; 32 33 # Load modular configuration files from the /etc/nginx/conf.d directory. 34 # See http://nginx.org/en/docs/ngx_core_module.html#include 35 # for more information. 36 include /etc/nginx/conf.d/*.conf; 37 38 server { 39 listen 8001 default_server; 40 listen [::]:8001 default_server; 41 server_name 10.2.1.92; 42 root /usr/share/nginx/html; 43 44 # Load configuration files for the default server block. 45 include /etc/nginx/default.d/*.conf; 46 47 location / { 48 proxy_pass http://127.0.0.1:8000; 49 proxy_set_header Host $host; 50 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 51 } 52 53 error_page 404 /404.html; 54 location = /40x.html { 55 } 56 57 error_page 500 502 503 504 /50x.html; 58 location = /50x.html { 59 } 60 } 61 62 # Settings for a TLS enabled server. 63 # 64 # server { 65 # listen 443 ssl http2 default_server; 66 # listen [::]:443 ssl http2 default_server; 67 # server_name _; 68 # root /usr/share/nginx/html; 69 # 70 # ssl_certificate "/etc/pki/nginx/server.crt"; 71 # ssl_certificate_key "/etc/pki/nginx/private/server.key"; 72 # ssl_session_cache shared:SSL:1m; 73 # ssl_session_timeout 10m; 74 # ssl_ciphers HIGH:!aNULL:!MD5; 75 # ssl_prefer_server_ciphers on; 76 # 77 # # Load configuration files for the default server block. 78 # include /etc/nginx/default.d/*.conf; 79 # 80 # location / { 81 # } 82 # 83 # error_page 404 /404.html; 84 # location = /40x.html { 85 # } 86 # 87 # error_page 500 502 503 504 /50x.html; 88 # location = /50x.html { 89 # } 90 # } 91 92 }
很重要的調試命令:
1.配置nginx.conf文件前先備份原文件,以免配置錯誤,無法還源
2.使用命令檢查修改后的nginx.conf文件,是否有語法問題
sudo nginx -t #檢查配置是否正確
驗證配置是否正確
通過gunicorn啟動flask項目
首先切換到flask項目所在目錄,並且項目已經配置過了wsgi.py的啟動文件:
wsgi.py文件內容如下:
from app import create_app application = create_app() if __name__ == '__main__': application.run()
激活帶gunicorn的虛擬環境
[root@67 flaskDemo]# lsvirtualenv automationVenv ============== flaskApi ======== rlcVenv ======= [root@67 flaskDemo]# workon flaskApi
確保nginx配置正確(看到successful字樣表示配置正確)
[root@67 nginx]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
在瀏覽器中輸入http://ip:nginx運行端口,看nginx啟動頁,表示nginx運行成功
重啟nginx命令:
[root@67 nginx]# service nginx restart Redirecting to /bin/systemctl restart nginx.service
接下來驗證配置結果:
在瀏覽器中輸入ip地址:http://10.2.1.92:8001/users
/users 是我在flask項目中已經定義好的頁面。
看到falsk項目視圖定義好的內容,即表示nginx+gunicorn的關系配置好了:

flaskDemo項目地址:
https://github.com/wangju003/flaskDemo.git
參考文檔:
https://baijiahao.baidu.com/s?id=1616440047552092518&wfr=spider&for=pc
https://www.jianshu.com/p/5600af9ff238
