nginx gunicorn:部署多个Flask应用程序

参见英文答案 >
Add a prefix to all Flask routes                                    10个

我已经在Arch Linux板上问了这个问题,但没有得到答案.所以我在这里试试运气:

我正在尝试在我的Arch Linux服务器上设置nginx gunicorn来运行多个Flask应用程序.但是我似乎没有以正确的方式配置nginx.
当我刚拿到一个Flask应用程序并运行时,一切似乎都运行良好.我在/etc/nginx/nginx.conf中包含了/ etc / nginx / sites-available和/ etc / nginx / sites-enabled.
我在/ etc / nginx / sites / available中创建了一个文件“flask_settings”,并将其链接到/ etc / nginx / sites-enabled.该文件如下所示:

server {
    location /{
            proxy_pass http://127.0.0.1:8000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;

}


}

我有一个包含我的Flask应用程序的文件夹(示例App,hellp.py),我在虚拟环境中使用gunicorn运行.我只是用它来运行它

gunicorn hello:app

如果我访问我的服务器IP,我可以访问该文件和不同的路由.
现在我尝试设置另一个应用程序,在/ etc / nginx / sites-enabled中创建另一个名为flask2的文件.它看起来像这样:

server {
    location /hello {
            proxy_pass http://127.0.0.1:8001;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;

}


}

然后我尝试在其自己的虚拟环境中运行应用程序

   gunicorn --bind 127.0.0.1:8001 hello:app

当我之后重新启动nginx时,我仍然能够访问第一个应用程序及其所有路由,但如果我尝试通过输入我的服务器IP来访问另一个路由器(在“/”之后),nginx总是告诉我,无法找到网站.我在这里错过了什么吗?
任何帮助都非常感谢.
提前致谢!

最佳答案 您应该为这两个应用分配单独的代理位置.

也就是说,有一个nginx配置文件,但每个路由有多个位置,或者你可以为每个路由分别配置conf文件.

例如:
h1.example.com使用端口号代理到所需地址的位置
h2.example.com代理到第二个应用程序的位置.

点赞