在ec2上部署flask应用程序时,nginx没有重定向到gunicorn

djmepvbi  于 2023-10-17  发布在  Nginx
关注(0)|答案(1)|浏览(122)

Gunicorn服务文件:

[Unit]
     Description=Gunicorn instance for a simple hello world app
     After=network.target
     [Service]
     User=ubuntu
     Group=www-data
     WorkingDirectory=/home/ubuntu/algotrading/oops
     ExecStart=/home/ubuntu/algotrading/venv/bin/gunicorn -b localhost:8000 wsgi:app
     Restart=always
     [Install]
     WantedBy=multi-user.target

/etc/nginx/sites-available/default文件,代理已添加

location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            proxy_pass http://127.0.0.1:8000;
            try_files $uri $uri/ =404;
    }

curl http://127.0.0.1:8000/oops/configuration/fetchConfigurations?schema=oops1_v1_sit工作正常,但是http://<public_ip>:8000/oops/configuration/fetchchbox?schema=oops1_v1_sit无法正常工作

fd3cxomn

fd3cxomn1#

我可以通过在/etc/nginx/nginx.conf文件中将user替换为root来解决这个问题。
还有一种方法可以设置nginx和gunicorn:

  1. gunicorn.service文件(位置:/etc/systemd/system/):
[Unit]
Description=Gunicorn instance to serve algotrading
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/algotrading/oops
Environment="PATH=/home/ubuntu/algotrading/venv/bin"
ExecStart=/home/ubuntu/algotrading/venv/bin/gunicorn --workers 3 --bind unix:algotrading.sock -m 007 wsgi:app

Restart=always
[Install]
WantedBy=multi-user.target
  1. Nginx配置文件(位置:/etc/nginx/sites-available/):
server {
  listen 80;
  server_name <Public IP>;

  location / {
  include proxy_params;
  proxy_pass http://unix:/home/ubuntu/algotrading/oops/algotrading.sock;
 }
}

1.基本命令:启动Gunicorn服务:

sudo systemctl daemon-reload
sudo systemctl start algotrading
sudo systemctl enable algotrading
sudo systemctl status algotrading

重新启动nginx:sudo systemctl restart nginx

相关问题