Flask+nginx+Gunicorn部署

当我们开发完Flask项目后是不能直接通过命令启动服务来使用的(扛不住的)

Gunicorn 

是一个给 UNIX 用的 WSGI HTTP 服务器。这是一个从 Ruby 的 Unicorn 项目移植的 pre-fork worker 模式。它既支持 eventlet ,也 支持 greenlet 。在这个服务器上运行 Flask 应用是相当简单的,该服务器支持高并发,所以我们现在使用gunicorn来需要我们的flask程序

安装gunicorn

   pip3 install gunicorn

使用gunicorn来启动flask程序

  gunicorn命令可通过gunicorn -h来查看

  常用的命令 

    --access-logfile FILE  The Access log file to write to. [None]

    -b ADDRESS, --bind ADDRESS The socket to bind. [['127.0.0.1:8000']]

    -w INT, --workers INT The number of worker processes for handling requests

  gunicorn -w 4 -b 127.0.0.1:4000 myproject:app
  表示启动 4个work进程,绑定在 127.0.0.1:4000,myproject 表示flask实例所在的py文件,app表示 flask实例变量名

使用nginx

# 定义负载均衡的池子
    upstream flask {
         server 127.0.0.1:4000; # 添加一个server,可添加多个
        }
    
    server {
        listen       80;
        server_name  192.168.25.35;
        
       location /static/~(.*)(\.jpg|\.png|\.gif|\.jepg|\.css|\.js|\.css){
                alias   html;
        }

        location / {
                 proxy_pass http://flask; # 将请求192.168.25.35的http转发到upstream池子的server
           proxy_set_header   Host             $host;
              proxy_set_header   X-Real-IP $remote_addr;       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         }

访问 http://192.168.25.35/,完成!

可参考 http://www.pythondoc.com/flask/deploying/wsgi-standalone.html#gunicorn

更多分享请关注微信公众号

 

猜你喜欢

转载自www.cnblogs.com/yaoqingzhuan/p/10891464.html