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
关于遇到的问题

1.开启nginx后静态资源访问出现403报错
在这里插入图片描述
查阅资料发现可能是文件权限不足的问题,关闭nginx
修改nginx.conf的用户权限
在第一行加入

user root;

再次启动解决问题。

2.502 bad gateway

很简单,很可能就是因为监听的地址没有写对或者被监听的项目没有启动

部署参考

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

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐