运行 2 个 Gunicorn 应用程序和带有 Supervisord 的 Nginx
Running 2 Gunicorn Apps and Nginx with Supervisord
诚然,这个问题困扰了我几个月。我只是拖延修复其他错误并将其搁置到现在必须修复的地方 --
我正在尝试 运行 2 个单独的 gunicorn 应用程序并在同一个 supervisord.conf 文件中启动 nginx。当我启动 supervisor 时,我能够成功 运行 handlecalls 应用程序但是当我转到 commentbox 负责加载的网站时,我收到内部服务错误 (500)。
当我 运行 handlecalls 和 commentbox 应用程序与命令字段后面的命令分开时,应用程序 运行 很好。为什么当我尝试 运行 同时使用 supervisord 时评论框程序给我一个 500 错误?
我的监督脚本:
[program:nginx]
directory = /var/www/vmail
command = service nginx start -g "daemon off;"
autostart = True
[program:commentbox]
directory = /var/www/vmail
command = gunicorn app:app -bind 0.0.0.0:8000
autostart = True
[program:handlecalls]
directory = /var/www/vmail
command = gunicorn handle_calls:app --bind 0.0.0.0:8000
autostart = True
[supervisord]
directory = /var/www/vmail
logfile = /var/www/vmail/supervisorerrs.log
loglevel = trace
这与supervisord无关。 Supervisord 只是您 start/stop/restart 服务器的一种方式。这与您的服务器配置有更多关系。
基础:要使用 nginx 为两个 gunicorn 应用程序提供服务,您必须 运行 它们在两个不同的端口上,然后将 nginx 配置为 proxy_pass 将请求发送到它们各自的端口。原因是:一旦一个进程 运行 连接在一个端口上,该端口就不能被另一个进程使用。
因此将您的 supervisord 脚本中的配置更改为:
[program:commentbox]
directory = /var/www/vmail
command = gunicorn app:app --bind 0.0.0.0:8000
autostart = True
[program:handlecalls]
directory = /var/www/vmail
command = gunicorn handle_calls:app --bind 0.0.0.0:8001
autostart = True
然后在你的 nginx 服务器的配置中 handlecalls
proxy_pass 127.0.0.1:8081
更新:这是部署 Web 应用程序的基础知识
- 如前所述,一个端口只能被一个进程监听
- 你可以使用nginx作为http服务器,监听端口
80
(或https的443
),然后将请求传递给监听其他端口的其他应用程序(例如,commentbox
在端口 8000
上处理调用在端口 8001
)
您可以通过在 /etc/nginx/sites-available/
中添加某些服务器配置文件来向 nginx 添加规则,例如如何为您的应用程序提供服务(默认情况下。在某些情况下会有所不同)。规则应该指定一种方式让 nginx 知道它应该将请求发送到哪个应用程序,例如:
- 要重复使用相同的 http 端口 (
80
),每个应用程序都应分配给不同的域。即:commentbox.yourdomain.com
代表 commentbox
和 handlecalls.yourdomain.com
代表 handlecalls
- 在同一域中为两个不同的应用程序提供服务的一种方法是让它们在不同的端口上提供服务。例如:
yourdomain.com
将服务于 commentbox
,而 yourdomain.com:8080
将服务于 handlecalls
- 在同一域和相同端口上为两个不同的应用程序提供服务的一种方法是让它们在两个不同的端点上提供服务。例如
yourdomain.com/commentbox
将服务于 commentbox
而 yourdomain.com/handlecalls
将服务于 handlecalls
将配置文件添加到 /etc/nginx/sites-available/
后,您必须将这些文件符号链接到 /etc/nginx/sites-enabled/
,好吧,告诉 nginx 您要启用它们。您可以将文件直接添加到 /etc/nginx/sites-enabled/
,但我不推荐这样做,因为它不会为您提供 enable/disable 您的应用程序的便捷方式。
更新:以下是如何配置 nginx 以使用两个不同的子域为 gunicorn 应用程序提供服务:
- 添加两个子域
commentbox.yourdomain.com
和 handlecalls.yourdomain.com
,并将它们都指向您服务器的 IP。
在 /etc/nginx/sites-available/commentbox
为 commentbox
创建配置文件,内容如下(适当编辑):
server {
listen 80;
server_name commentbox.yourdomain.com;
root /path/to/your/application/static/folder;
location / {
try_files $uri @app;
}
location @app {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_pass http://127.0.0.1:8000;
}
}
在 /etc/nginx/sites-available/handlecalls
为 handlecalls
创建配置文件,内容如下(适当编辑):
server {
listen 80;
server_name handlecalls.yourdomain.com;
root /path/to/your/application/static/folder;
location / {
try_files $uri @app;
}
location @app {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_pass http://127.0.0.1:8001;
}
}
创建符号链接以启用这些服务器:
sudo ln -s /etc/nginx/sites-available/commentbox /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/handlecalls /etc/nginx/sites-enabled/
重启nginx生效
sudo service nginx restart
诚然,这个问题困扰了我几个月。我只是拖延修复其他错误并将其搁置到现在必须修复的地方 --
我正在尝试 运行 2 个单独的 gunicorn 应用程序并在同一个 supervisord.conf 文件中启动 nginx。当我启动 supervisor 时,我能够成功 运行 handlecalls 应用程序但是当我转到 commentbox 负责加载的网站时,我收到内部服务错误 (500)。
当我 运行 handlecalls 和 commentbox 应用程序与命令字段后面的命令分开时,应用程序 运行 很好。为什么当我尝试 运行 同时使用 supervisord 时评论框程序给我一个 500 错误?
我的监督脚本:
[program:nginx]
directory = /var/www/vmail
command = service nginx start -g "daemon off;"
autostart = True
[program:commentbox]
directory = /var/www/vmail
command = gunicorn app:app -bind 0.0.0.0:8000
autostart = True
[program:handlecalls]
directory = /var/www/vmail
command = gunicorn handle_calls:app --bind 0.0.0.0:8000
autostart = True
[supervisord]
directory = /var/www/vmail
logfile = /var/www/vmail/supervisorerrs.log
loglevel = trace
这与supervisord无关。 Supervisord 只是您 start/stop/restart 服务器的一种方式。这与您的服务器配置有更多关系。
基础:要使用 nginx 为两个 gunicorn 应用程序提供服务,您必须 运行 它们在两个不同的端口上,然后将 nginx 配置为 proxy_pass 将请求发送到它们各自的端口。原因是:一旦一个进程 运行 连接在一个端口上,该端口就不能被另一个进程使用。
因此将您的 supervisord 脚本中的配置更改为:
[program:commentbox]
directory = /var/www/vmail
command = gunicorn app:app --bind 0.0.0.0:8000
autostart = True
[program:handlecalls]
directory = /var/www/vmail
command = gunicorn handle_calls:app --bind 0.0.0.0:8001
autostart = True
然后在你的 nginx 服务器的配置中 handlecalls
proxy_pass 127.0.0.1:8081
更新:这是部署 Web 应用程序的基础知识
- 如前所述,一个端口只能被一个进程监听
- 你可以使用nginx作为http服务器,监听端口
80
(或https的443
),然后将请求传递给监听其他端口的其他应用程序(例如,commentbox
在端口8000
上处理调用在端口8001
) 您可以通过在
/etc/nginx/sites-available/
中添加某些服务器配置文件来向 nginx 添加规则,例如如何为您的应用程序提供服务(默认情况下。在某些情况下会有所不同)。规则应该指定一种方式让 nginx 知道它应该将请求发送到哪个应用程序,例如:- 要重复使用相同的 http 端口 (
80
),每个应用程序都应分配给不同的域。即:commentbox.yourdomain.com
代表commentbox
和handlecalls.yourdomain.com
代表handlecalls
- 在同一域中为两个不同的应用程序提供服务的一种方法是让它们在不同的端口上提供服务。例如:
yourdomain.com
将服务于commentbox
,而yourdomain.com:8080
将服务于handlecalls
- 在同一域和相同端口上为两个不同的应用程序提供服务的一种方法是让它们在两个不同的端点上提供服务。例如
yourdomain.com/commentbox
将服务于commentbox
而yourdomain.com/handlecalls
将服务于handlecalls
- 要重复使用相同的 http 端口 (
将配置文件添加到
/etc/nginx/sites-available/
后,您必须将这些文件符号链接到/etc/nginx/sites-enabled/
,好吧,告诉 nginx 您要启用它们。您可以将文件直接添加到/etc/nginx/sites-enabled/
,但我不推荐这样做,因为它不会为您提供 enable/disable 您的应用程序的便捷方式。
更新:以下是如何配置 nginx 以使用两个不同的子域为 gunicorn 应用程序提供服务:
- 添加两个子域
commentbox.yourdomain.com
和handlecalls.yourdomain.com
,并将它们都指向您服务器的 IP。 在
/etc/nginx/sites-available/commentbox
为commentbox
创建配置文件,内容如下(适当编辑):server { listen 80; server_name commentbox.yourdomain.com; root /path/to/your/application/static/folder; location / { try_files $uri @app; } location @app { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_redirect off; proxy_pass http://127.0.0.1:8000; } }
在
/etc/nginx/sites-available/handlecalls
为handlecalls
创建配置文件,内容如下(适当编辑):server { listen 80; server_name handlecalls.yourdomain.com; root /path/to/your/application/static/folder; location / { try_files $uri @app; } location @app { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_redirect off; proxy_pass http://127.0.0.1:8001; } }
创建符号链接以启用这些服务器:
sudo ln -s /etc/nginx/sites-available/commentbox /etc/nginx/sites-enabled/ sudo ln -s /etc/nginx/sites-available/handlecalls /etc/nginx/sites-enabled/
重启nginx生效
sudo service nginx restart