Flask+Gunicorn+Nginx配置多个app
前两天在vps上面部署了自己的两个flask应用,搭建架构为flask+gunicorn+nginx,在这边做个备忘。
请自行安装好nginx、gunicorn和flask的运行环境,这里不再赘述
Nginx配置
修改nginx的配置
vi /etc/nginx/sites-available/default
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//default server { listen 80;//监听80端口 server_name www.ikiyomi.cc; access_log /root/web/logs/nginx_app.log; //转发第一个app的请求 location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header host $http_host; proxy_pass http://127.0.0.1:5000; } //转发第二个app的请求 location /weixin { proxy_pass http://127.0.0.1:5001; } } |
测试配置文件是否产生错误
nginx -t
开启服务
service nginx start
Flask app配置
将两个app运行的端口与nginx定义的端口一致,本地运行
1 2 3 4 5 6 7 8 9 10 |
//app_1 if __name__ == '__main__': app.run(debug=False, port=5000) # 127.0.0.1:5000 //app_2 if __name__ == '__main__': app.run(debug=False, port=5001) # 127.0.0.1:5001 |
Gunicorn配置
Gunicorn配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//gunicorn_app1.conf //配置方式相似,以app_1为例 # 绑定5000端口,与Flask端口一致 bind = "127.0.0.1:5000" # 线程数 workers = 3 backlog = 2048 worker_class = "gevent" # debug = True proc_name = "gunicorn.pid" pidfile = "/tmp/gunicorn.pid" # logfile = "/root/wechat/logs/20161019.log" accesslog = "/root/wechat_web/logs/gunicorn.log" loglevel = "debug" # 是否以守护进程方式运行 daemon = True |
运行Gunicorn
gunicorn -c gunicorn_app1.conf app_1:app
参数说明:
* -c 以配置文件方式运行
* app_1 文件名
* app app名
查看gunicorn守护进程是否运行
ps aux | grep gunicorn
链接:https://www.ioiogoo.cn/2016/12/07/flaskgunicornnginx配置多个app/
本站所有文章除特殊说明外均为原创,转载请注明出处!
超人超
不是两个应用没,为什么只启动了一个?