欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

nginx + gunicorn + flask项目发布

程序员文章站 2022-03-18 16:49:41
程序安装(linux mint) 安装: 安装: 配置 nginx默认配置信息在 flask项目部署 复制flask程序至 启动gunicorn: ,` w mission manager flask py`文件 启动 服务:`service nginx start` ......

程序安装(linux mint)

  • gunicorn安装:pip install gunicorn
  • nginx安装:sudo apt-get install nginx

配置

nginx默认配置信息在/etc/nginx/sites-enabled/default

server {
        listen 8300 default_server;#默认端口设置位置
        listen [::]:8300 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.html index.htm;

        # make site accessible from http://localhost/
        server_name localhost;

        location / {
                # first attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }
server {
        listen 8200;#nginx对外监听端口设置
        server_name my.com;#网站域名绑定
        #root html;
        #index index.html index.htm;

        location /auth {#auth为flask程序你的蓝图,如果没有设置蓝图则为/
                #try_files $uri $uri/ =404;
                proxy_pass http://0.0.0.0:8100;#gunicorn启动flask的网址
                proxy_set_header host $host;
                proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
                fastcgi_connect_timeout 300;
                fastcgi_send_timeout 300;
                fastcgi_read_timeout 300;
                client_max_body_size 50m;#服务器上传文件大小上限
        }
        location /static {#静态文件static文件夹所在目录
                root /var/www/mission-manager/;
        }
}

flask项目部署

  • 复制flask程序至/var/www/
  • 启动gunicorn:gunicorn -w 2 -b 127.0.0.0:8100 mission-manager:app,-w进程数量,mission-managerflask程序主函数所在py文件
  • 启动nginx服务:service nginx start