基于flask框架博客线上部署过程

1:为什么使用nginx作为反向代理

接上篇:基于flask框架的博客线上部署过程---(1)

gunicron虽然可以直接对公网提供http请求,但是功能上远没有nginx丰富,如http请求的过滤,针对不同请求头做不同业务的分发,内网多主机服务的负载均衡。这些都是nginx的优势,所以将nginx暴露在公网ip下,直接处理http请求是更为妥当的策略。

2:云主机上nginx软件的安装

#centos
sudo yum install nginx

#ubuntu
sudo apt install nginx

3:nginx的配置

#centos上是对/etc/nginx/nginx.conf文件进行配置
#需要改变的地方非常少,主要是server域下面的三个地方:(如下)
# 1:listen 80  监听公网ip的80端口(记得打开centos和waf的端口防火墙)
# 2:server_name 后面跟上自己购买的域名,如果没有,直接使用该机的public ip
# 3: proxy_pass  后面跟上启动gunicorn时,-b参数绑定的地址(不要使用0.0.0.0,这样会将gunicorn的8080端口直接暴露在公网ip下)
server {
    listen 80;
    server_name example.org; # 这是HOST机器的外部域名,用地址也行

    location / {
        proxy_pass http://127.0.0.1:8080; # 这里是指向 gunicorn host 的服务地址
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

  }

4:nginx和gunicorn的启动

#centos
source /blogenv/bin/activate   
cd 到 wsgi的目录
gunicorn -w 4 -b 127.0.0.1:8080  wsgi:app
systemctl start nginx   #没报错就是正常的

#ubuntu
source /blogenv/bin/activate   
cd 到 wsgi的目录
gunicorn -w 4 -b 127.0.0.1:8080  wsgi:app
sudo service nginx restart  #没报错就是正常的

#查看nginx是否正常运行
ps aux|grep ngix
#通过wget or browser访问网站是否正常工作

5:后续应该把开启gunicorn的服务添加到系统控制命令中

#centos-systemctl

#ubuntu-service

6:请求响应的处理过程

图片描述

7:开源flask博客地址

https://github.com/huangtao00...

相关推荐