對於gunicorn+nginx的配置,理解他們之間的關係很重要,以及最後如何確認配置結果是正確的也很重要css
nginx 配置文件:html
修改這個配置文件有3個用處:node
假設服務器自己的Ip是A稱爲ip-A,而我用gunicorn啓動flask時寫的ip是127.0.0.1,用ip-B表示nginx
1.當我在瀏覽器輸入http://ip-A:端口(nginx的端口) 時,nginx會把訪問地址指向http://ip-B:端口(gunicorn啓動的端口)git
因此咱們頁面看到的內容實際是gunicorn啓的flask的根頁面,github
即視圖函數中app.route('/')裝飾器所裝飾的函數所返回頁面的內容django
那麼這種映身關係在nginx.conf配置文件中如何配置呢?flask
主要就是對proxy_pass進行配置瀏覽器
參數說明:服務器
完整的配置文件在這裏做個備份:
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