gunicorn+nginx配置方法

对于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

posted @ 2019-09-24 14:03  wangju003  阅读(5514)  评论(0编辑  收藏  举报