将 Nginx 端口转发到 gunicorn 实例

Forwarding Nginx port to gunicorn instance

我正在按照本教程设置我的烧瓶服务器。

https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-18-04#step-6-%E2%80%94-securing-the-application

当我进行到第 6 步时,我看到他们正在为整个 url 设置烧瓶,但我想将其指向特定端口。

这是我的 nginx 代码。这当前会产生 404。

server {
        listen 5000;
        server_name site.com;

        location / {
                include proxy_params;
                proxy_pass http://unix:/home/user/project/project.sock;
        }
}

其他文件与教程相同。我试图修改 .sock 文件,但它似乎是自动生成的,无法修改。此外,在我担心从 gunicorn 处理它之前,我需要找到一种方法让 nginx 处理它。

我的最终目标是当请求发送到 0.0.0.0:5000 时让 nginx 转发请求到 flask 运行,并让所有其他请求 0.0.0.0 , 0.0.0.0/* 由 nginx 处理。

任何有助于理解这一切的帮助在这一点上都非常感谢。

编辑

我在可用站点中的 nginx 配置

server {
     server_name domain www.domain; 
     location / {
         include proxy_params; 
         proxy_pass http://127.0.0.1:8080/; 
     }
}

如果你想让 flask 打开一个端口而不是一个文件,你应该覆盖 [service]

[service]
...
ExecStart=/home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind 127.0.0.1:8000 -m 007 wsgi:app

并将您的 nginx 配置更改为 proxy_pass http://127.0.0.1:8000/;

这样您就可以访问 port 8000 以检查 gunicornflask 的工作情况。请记住小心防火墙规则以保护 port 8000。要就哪个更好进行讨论,您可以尝试:gunicorn + nginx: Server via socket or proxy?