gunicorn + nginx configuration

For gunicorn + nginx configuration, understand the relationship between them is very important, and, finally, how to verify the configuration is correct is also important

nginx configuration file:

Modify the configuration file has three useful:

Assuming the server itself is A Ip called ip-A, and I used to write when gunicorn started flask ip is 127.0.0.1, represented by ip-B

1. When I type in the browser http: // ip-A: port (nginx port), nginx will visit the address points to http: // ip-B: Port (gunicorn start port)

So we see that the page content is actually gunicorn Kai flask root page,

That view function app.route ( '/') decorator decorated function returns the contents of the page

 

Then this relationship reflect the body how to configure nginx.conf configuration file it?

The main is to configure proxy_pass

Parameter Description:

  • listen listening port is needed
  • server_name is the domain name to be bound, temporarily without a domain, use the ip
  • location / when access to the root of the time, forwards all requests to 127.0.0.1:8000, so forwarded to herein gunicorn django application started, the intermediate configuration is to be forwarded, the content, to meet the most basic needs of the foregoing, For special requirements, please see the official documentation on their own nginx
  • location / static / configure the path where static files, static files nginx by the processing, dynamic forwarded to django, if not all js css configuration will not find the referenced sites

Full configuration file to make a backup here:

 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 }

 

 

It is important debug commands:

1. Front nginx.conf configuration files to back up the original file, in order to avoid configuration errors, can not also source

2. Use the command to check nginx.conf modified file, whether there is a syntax problem

nginx -t sudo # to check the configuration

Verify the configuration is correct

Start flask project by gunicorn

First, switch to the directory where the flask project, and the project is already configured wsgi.py startup file:

wsgi.py document reads as follows:

from app import create_app

application = create_app()

if __name__ == '__main__':
    application.run()

Activate the virtual environment with gunicorn

[root @ 67 bottle demo] # lsvirtualenv 
automationVenv
 ============== 


flaskApi
 ======== 


rlcVenv
 ======= 


[root @ 67 bottle demo] # workon flaskApi

 

Nginx configuration to ensure correct (see the word represents the successful configured correctly)

[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

Enter in your browser http: // ip: nginx running port, see nginx start page indicates a successful run nginx

Nginx restart command:

[root@67 nginx]# service nginx restart
Redirecting to /bin/systemctl restart nginx.service

Next to verify the configuration:

Enter the ip address in your browser: http: //10.2.1.92: 8001 / users

/ Users I've defined page flask project.

See falsk project view defined content, it means that the relationship between nginx + gunicorn good configuration:

 

 

 

flaskDemo Project Address:

https://github.com/wangju003/flaskDemo.git

Reference documents:

https://baijiahao.baidu.com/s?id=1616440047552092518&wfr=spider&for=pc 

https://www.jianshu.com/p/5600af9ff238

Guess you like

Origin www.cnblogs.com/kaerxifa/p/11576567.html