If you haven't seen the last article, please click here first.
Python 3 Flask+nginx+Gunicorn Deployment (Part I)
Continue with the previous article, in which we have run and installed gunicorn and nginx. In this article, we need to bind the nginx and gunicorn ports.
In the last article, we've run gunicorn
Guicorn-w 4-b 127.0.0.1:8000 Entry File Name: app
nginx operation directory:
/usr/local/nginx/sbin
Nginx can be restarted, overloaded, stopped and started in the / usr/local/nginx/sbin directory.
Here we pass through the nginx reverse proxy website domain name and server, there may be some twists.
Popular point:
It is someone who visits our website and sends the request to flask server through nginx reverse proxy.
First, let's set up the configuration file for nginx
cd /usr/local/nginx/conf
vi nginx.conf
Then open the configuration file:
#gzip on;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
server {
listen 80;
server_name www.00reso.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
# index index.html index.htm;
proxy_pass http://127.0.0.1:8000;
access_log /home/admin/api_access.log;
proxy_read_timeout 60;
error_log /home/admin/api_error.log;
}
The main areas of action are:
#gzip on;
# 1 Agent settings, this is how they are set up
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 2 Binding Domain Names
server_name www.00reso.com;
# 3. Monitor the port of this server, set log, request time, etc.
proxy_pass http://127.0.0.1:8000;
access_log /home/admin/api_access.log;
proxy_read_timeout 60;
error_log /home/admin/api_error.log;
Then save it.
Next, overload the nginx file under / usr/local/nginx/sbin/nginx
Use commands:
sudo nginx -t
If you see
1 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2 nginx: configuration file /etc/nginx/nginx.conf test is successful
It means success.
Restart nginx
sudo service nginx restart
The command to restart nginx here may not work. At this time, you use
Ps-e aux | grep nginx kills the process of nginx.
Then start nginx again in the / usr/local/nginx/sbin directory
nginx
If you see:
[admin@izbp11722iq94py8qx691sz conf]$ ps -e aux|grep nginx
root 2657 0.0 0.0 24952 784 ? Ss Jun25 0:00 nginx: master process nginx
nobody 2658 0.0 0.1 25388 1752 ? S Jun25 0:00 nginx: worker process
admin 13814 0.0 0.0 112664 968 pts/2 S+ 20:59 0:00 grep --color=auto nginx
In this case, the nginx startup is successful.
Verification
1 Use local ping
curl localhost:80
If the content of the flask index page appears, the configuration of nginx and gunicorn is successful.
2. Remote direct input www.00reso.com to see if it can be accessed. If there are no failed pages, the access will be successful. If there are failed pages, most of them are firewalls or 80 ports closed. Here we need to open the firewall.
Edit firewall whitelist
[root@localhost ~]# vim /etc/sysconfig/iptables Add the following line of code -A INPUT -p tcp -m state -- state NEW -m tcp --dport 80 -j ACCEPT Save Exit and Restart Firewall [root@localhost ~]# service iptables restart
This allows access.
Welcome to my personal website:
www.00reso.com
At present, I am still exploring, and now in the optimization of fast horse and whip.
Because flask+nginx+gunicorn deployed itself to trample through many pits, but trampled from the pit, almost all understand. Fortunately, I haven't given up.~~
More help and discussion are needed.
Self-study group QQ exchange group: 702689263
Add me a message and pull you into the group:
hll643435675
Thank you for the following references:
https://www.cnblogs.com/shenckicc/p/7003242.html?utm_source=itdadao&utm_medium=referral
https://www.cnblogs.com/taiyonghai/p/6728707.html
http://www.vuln.cn/8832