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

1:为何使用nginx做为反向代理

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

gunicron虽然能够直接对公网提供http请求,可是功能上远没有nginx丰富,如http请求的过滤,针对不一样请求头作不一样业务的分发,内网多主机服务的负载均衡。这些都是nginx的优点,因此将nginx暴露在公网ip下,直接处理http请求是更为稳当的策略。git

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:请求响应的处理过程

图片描述github

7:开源flask博客地址

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

相关文章
相关标签/搜索