python web app 部署比如 flask 一般都会和 Gunicorn 一起配合,和django 一般都会和 uwsgi 一起,有人可能要问了,直接使用nginx 反向代理到django 或者flask 不就行了吗,为什么还需要nginx,或者是直接使用uwsgi+django,flask + Gunicorn 直接部署不行吗,为啥还要nginx
python web部署 为什么需要uwsgi 或者 Gunicorn
简单来说,如果你只是使用nginx 反向代理,甚至是直接一个django,flask 裸跑的话,由于python GIL的存在web app 性能会很低,而uwsgi或者是Gunicorn 可以同时开多个django 或者flask 进程通过这样达到提升并发的效果
python web 部署为什么需要nginx
直接使用uwsgi 或者是 Gunicorn 同样可以部署python web 但是以目前前后端分离的开发环境下,nginx 处理静态资源会比 uwsgi 和 Gunicorn 强得多
django + uwsgi + nginx 的配置
django 的uwsgi.ini 配置我就不必要晒出来了,大家都会这里主要是nginx 端的配置
location ~^/robot/ {
include /etc/nginx/uwsgi_params;
uwsgi_pass localhost:8080;
root /var/www/rbp-html;
index index.html;
proxy_redirect off;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_connect_timeout 1h;
proxy_read_timeout 1h;
proxy_send_timeout 1h;
send_timeout 1h;
proxy_max_temp_file_size 5096m;
client_max_body_size 300M;
sendfile on;
}
最主要是这两行配置
include /etc/nginx/uwsgi_params;
uwsgi_pass localhost:8080;
这个是nginx 将请求转发至uwsgi 的配置