微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nginx 部署flask应用

Nginx 部署项目不要想的太过复杂,直接上手操作,遇到问题再逐个击破
编程语言:python
项目后端:flask
前端:vue 2.x

准备工作

需要掌握的命令

sudo Nginx -c Nginx.conf :指定配置文件启动Nginx

Nginx -s stop  :快速停止Nginx

Nginx -s quit  :完整有序的停止Nginx

start Nginx : 启动Nginx

Nginx -s reload  :修改配置后重新加载生效

Nginx -s reopen  :重新打开日志文件

Nginx -t -c /path/to/Nginx.conf 测试Nginx配置文件是否正确

配置好Nginx.conf

以下配置文件中的路径都采用的绝对路径

user root;
events {
  worker_connections 1024; # increase if you have lots of clients
  accept_mutex off; # set to 'on' if Nginx worker_processes > 1
  # 'use epoll;' to enable for Linux 2.6+
  # 'use kqueue;' to enable for FreeBSD, OSX
}

http {
  include /etc/Nginx/mime.types;
  # fallback in case we can't determine a type
  default_type application/octet-stream;
  access_log /home/f7689984/NginxLog/access.log combined;
  sendfile on;

  upstream app_server {
    # fail_timeout=0 means we always retry an upstream even if it Failed
    # to return a good HTTP response

    # for UNIX domain socket setups
    server unix:/tmp/gunicorn.sock fail_timeout=0;

    # for a TCP configuration
    # server 192.168.0.7:8000 fail_timeout=0;
  }

  server {
    # use 'listen 80 deferred;' for Linux
    # use 'listen 80 accept_filter=httpready;' for FreeBSD
    # 监听80端口访问并转发到http://10.134.82.195:8000
    listen 80;
    client_max_body_size 4G;

    # set the correct host(s) for your site
    server_name 10.134.82.195;

    keepalive_timeout 5;

    # path for static files
    # root /app/static;

    location / {
      root /home/f7689984/bm-hub/app/dist;
      proxy_pass http://10.134.82.195:8000;
      index index.html;
    }

    location /static/ {
        root /home/f7689984/bm-hub/app/;
        autoindex off;
    }

    # error_page 500 502 503 504 /500.html;
    # location = /500.html {
    #  root /path/to/app/current/public;
    # }
  }
}

具体步骤

1.将Nginx安装好
2.启动flask应用

# 使用nohup后台gunicorn启动flask应用
nohup gunicorn -c gunicorn.conf.py run:app >/dev/null 2>&1 &

3.编辑好Nginx配置文件并启动

sudo Nginx -c Nginx.conf

关于遇到的问题

开启Nginx后静态资源访问出现403报错

在这里插入图片描述

查阅资料发现可能是文件权限不足的问题,关闭Nginx
修改Nginx.conf的用户权限
在第一行加入

user root;

再次启动解决问题。

部署参考

https://qizhanming.com/blog/2018/08/06/how-to-install-Nginx-on-centos-7

https://www.digitalocean.com/community/tutorials/how-to-install-Nginx-on-centos-7

https://wizardforcel.gitbooks.io/the-way-to-flask/content/chapter013.html

https://stackoverflow.com/questions/42329261/running-Nginx-as-non-root-user

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐