如何使用 Supervisor(包括 Gunicorn 和 Nginx)设置多个 Django 应用程序? bind() 到 [::]:8090 失败(98:地址已在使用中)
How can I setup multiple Django Apps using Supervisor (including Gunicorn and Nginx)? bind() to [::]:8090 failed (98: Address already in use)
我已经使用 Supervisor、Gunicorn 和 Nginx 部署了一个 Django/Wagtail 应用程序(在 Debian Buster 上),所以我可以使用 http://xx.xxx.xxx.xxx:8090。
/etc/nginx/sites-available/cms
server {
server_name xx.xxx.xxx.xxx;
listen 8090;
listen [::]:8090 ipv6only=on;
error_log /home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/var/log/gunicorn-error.log;
access_log /home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/var/log/gunicorn-access.log;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/www.mysite.com/www/my-site/cms/admin_panel;
}
location /media/ {
root /home/www.mysite.com/www/my-site/cms/admin_panel;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/run/gunicorn.sock;
}
}
/etc/supervisor/conf.d/guni-mysite-admin.conf
[program:guni-mysite-admin]
command=/home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/bin/gunicorn admin_panel.wsgi:application --config /home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/etc/gunicorn/conf.py
user=www.mysite.com
autostart=true
autorestart=true
/etc/supervisor/conf.d/nginx-mysite-admin.conf
[program:nginx-mysite-admin]
command=/usr/sbin/nginx -g "daemon off;"
autostart=true
autorestart=true
stderr_logfile=/home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/var/log/nginx-error.log
stdout_logfile=/home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/var/log/nginx-access.log
stderr_logfile_maxbytes=2MB
stdout_logfile_maxbytes=2MB
/home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/etc/gunicorn/conf.py
workers = 3
keepalive = 5
user = 'www.mysite.com'
proc_name = 'admin_panel'
loglevel = 'error'
errorlog = '/home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/var/log/gunicorn-error.log'
accesslog = '/home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/var/log/gunicorn-access.log'
bind = 'unix:/home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/run/gunicorn.sock'
raw_env = ['DJANGO_SETTINGS_MODULE=admin_panel.settings.production']
pythonpath = '/home/www.mysite.com/www/mysite/cms/admin_panel'
现在我以同样的方式添加了 2 个 Django 应用程序。不幸的是主管不能提出来。有时 3 次运行中有 1 次运行,但大多数时间 none 运行。如果它有效,它会创建 3 个进程(idk 如果它应该是这样的话)。
$ sudo lsof -i:8090
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 3631 root 16u IPv4 961301189 0t0 TCP *:8090 (LISTEN)
nginx 3631 root 17u IPv6 961301190 0t0 TCP *:8090 (LISTEN)
nginx 3632 www-data 16u IPv4 961301189 0t0 TCP *:8090 (LISTEN)
nginx 3632 www-data 17u IPv6 961301190 0t0 TCP *:8090 (LISTEN)
nginx 3633 www-data 16u IPv4 961301189 0t0 TCP *:8090 (LISTEN)
nginx 3633 www-data 17u IPv6 961301190 0t0 TCP *:8090 (LISTEN)
Nginx 错误日志给出 98: 地址已在使用,即使在端口 81 上(将其作为默认端口,因为 Apache 使用 80),但未使用。 Apache 应该不是问题,因为它不起作用,即使 Apache 已关闭。
/var/log/nginx/error.log
...
2021/08/06 12:41:54 [emerg] 24927#24927: bind() to [::]:4020 failed (98: Address already in use)
2021/08/06 12:41:54 [emerg] 24927#24927: bind() to [::]:8090 failed (98: Address already in use)
2021/08/06 12:41:54 [emerg] 24927#24927: bind() to [::]:81 failed (98: Address already in use)
2021/08/06 12:41:54 [emerg] 24927#24927: bind() to [::]:8070 failed (98: Address already in use)
2021/08/06 12:41:54 [emerg] 24927#24927: bind() to [::]:8080 failed (98: Address already in use)
2021/08/06 12:41:54 [emerg] 24927#24927: bind() to [::]:4030 failed (98: Address already in use)
2021/08/06 12:41:54 [emerg] 24928#24928: still could not bind()
...
输出 - $ sudo service nginx status
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2021-08-07 10:27:49 CEST; 50s ago
Docs: man:nginx(8)
Process: 25364 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 25366 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 25367 (nginx)
Tasks: 3 (limit: 4915)
Memory: 4.1M
CGroup: /system.slice/nginx.service
├─25367 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─25368 nginx: worker process
└─25369 nginx: worker process
Aug 07 10:27:49 xxxxxxx.server.linevast.org systemd[1]: Starting A high performance web server and a reverse proxy server...
Aug 07 10:27:49 xxxxxxx.server.linevast.org systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Aug 07 10:27:49 xxxxxxx.server.linevast.org systemd[1]: Started A high performance web server and a reverse proxy server.
输出 - $ sudo supervisorctl status all
Gunicorn 进程 运行 正常,Ngnix 进程尝试启动,但中断了。 Nginx 错误日志给出 bind() to [::]:8090 failed (98: Address already in use)
guni-csd-admin RUNNING pid 25564, uptime 0:06:44
guni-csd-phrases RUNNING pid 25567, uptime 0:06:44
guni-kc-admin RUNNING pid 25566, uptime 0:06:44
nginx-csd-admin STARTING
nginx-csd-phrases RUNNING pid 31118, uptime 0:00:01
nginx-kc-admin RUNNING pid 31103, uptime 0:00:02
解决方案
通过以下设置,我成功地 运行 两个客户端,三个 API´s,一个 Wordpress 和 Php 分析网站没有冲突:例如 API's 将 Apache2 HTTP 服务器作为代理(侦听端口 80)转发到 Nginx(不受 Supervisor 控制,这实际上是解决方案!)Gunicorn 由 Supervisor 控制。所以我就不再对 Nginx 进程使用 Supervisor 了。它可能在不使用 ipv6only 的情况下工作,但我想使用它是为了更好地控制 IP 绑定。所以我对此没意见。
我已经使用 Supervisor、Gunicorn 和 Nginx 部署了一个 Django/Wagtail 应用程序(在 Debian Buster 上),所以我可以使用 http://xx.xxx.xxx.xxx:8090。
/etc/nginx/sites-available/cms
server {
server_name xx.xxx.xxx.xxx;
listen 8090;
listen [::]:8090 ipv6only=on;
error_log /home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/var/log/gunicorn-error.log;
access_log /home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/var/log/gunicorn-access.log;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/www.mysite.com/www/my-site/cms/admin_panel;
}
location /media/ {
root /home/www.mysite.com/www/my-site/cms/admin_panel;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/run/gunicorn.sock;
}
}
/etc/supervisor/conf.d/guni-mysite-admin.conf
[program:guni-mysite-admin]
command=/home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/bin/gunicorn admin_panel.wsgi:application --config /home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/etc/gunicorn/conf.py
user=www.mysite.com
autostart=true
autorestart=true
/etc/supervisor/conf.d/nginx-mysite-admin.conf
[program:nginx-mysite-admin]
command=/usr/sbin/nginx -g "daemon off;"
autostart=true
autorestart=true
stderr_logfile=/home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/var/log/nginx-error.log
stdout_logfile=/home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/var/log/nginx-access.log
stderr_logfile_maxbytes=2MB
stdout_logfile_maxbytes=2MB
/home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/etc/gunicorn/conf.py
workers = 3
keepalive = 5
user = 'www.mysite.com'
proc_name = 'admin_panel'
loglevel = 'error'
errorlog = '/home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/var/log/gunicorn-error.log'
accesslog = '/home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/var/log/gunicorn-access.log'
bind = 'unix:/home/www.mysite.com/.local/share/virtualenvs/cms-WqsZ9qOt/run/gunicorn.sock'
raw_env = ['DJANGO_SETTINGS_MODULE=admin_panel.settings.production']
pythonpath = '/home/www.mysite.com/www/mysite/cms/admin_panel'
现在我以同样的方式添加了 2 个 Django 应用程序。不幸的是主管不能提出来。有时 3 次运行中有 1 次运行,但大多数时间 none 运行。如果它有效,它会创建 3 个进程(idk 如果它应该是这样的话)。
$ sudo lsof -i:8090
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 3631 root 16u IPv4 961301189 0t0 TCP *:8090 (LISTEN)
nginx 3631 root 17u IPv6 961301190 0t0 TCP *:8090 (LISTEN)
nginx 3632 www-data 16u IPv4 961301189 0t0 TCP *:8090 (LISTEN)
nginx 3632 www-data 17u IPv6 961301190 0t0 TCP *:8090 (LISTEN)
nginx 3633 www-data 16u IPv4 961301189 0t0 TCP *:8090 (LISTEN)
nginx 3633 www-data 17u IPv6 961301190 0t0 TCP *:8090 (LISTEN)
Nginx 错误日志给出 98: 地址已在使用,即使在端口 81 上(将其作为默认端口,因为 Apache 使用 80),但未使用。 Apache 应该不是问题,因为它不起作用,即使 Apache 已关闭。
/var/log/nginx/error.log
...
2021/08/06 12:41:54 [emerg] 24927#24927: bind() to [::]:4020 failed (98: Address already in use)
2021/08/06 12:41:54 [emerg] 24927#24927: bind() to [::]:8090 failed (98: Address already in use)
2021/08/06 12:41:54 [emerg] 24927#24927: bind() to [::]:81 failed (98: Address already in use)
2021/08/06 12:41:54 [emerg] 24927#24927: bind() to [::]:8070 failed (98: Address already in use)
2021/08/06 12:41:54 [emerg] 24927#24927: bind() to [::]:8080 failed (98: Address already in use)
2021/08/06 12:41:54 [emerg] 24927#24927: bind() to [::]:4030 failed (98: Address already in use)
2021/08/06 12:41:54 [emerg] 24928#24928: still could not bind()
...
输出 - $ sudo service nginx status
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2021-08-07 10:27:49 CEST; 50s ago
Docs: man:nginx(8)
Process: 25364 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 25366 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 25367 (nginx)
Tasks: 3 (limit: 4915)
Memory: 4.1M
CGroup: /system.slice/nginx.service
├─25367 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─25368 nginx: worker process
└─25369 nginx: worker process
Aug 07 10:27:49 xxxxxxx.server.linevast.org systemd[1]: Starting A high performance web server and a reverse proxy server...
Aug 07 10:27:49 xxxxxxx.server.linevast.org systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Aug 07 10:27:49 xxxxxxx.server.linevast.org systemd[1]: Started A high performance web server and a reverse proxy server.
输出 - $ sudo supervisorctl status all
Gunicorn 进程 运行 正常,Ngnix 进程尝试启动,但中断了。 Nginx 错误日志给出 bind() to [::]:8090 failed (98: Address already in use)
guni-csd-admin RUNNING pid 25564, uptime 0:06:44
guni-csd-phrases RUNNING pid 25567, uptime 0:06:44
guni-kc-admin RUNNING pid 25566, uptime 0:06:44
nginx-csd-admin STARTING
nginx-csd-phrases RUNNING pid 31118, uptime 0:00:01
nginx-kc-admin RUNNING pid 31103, uptime 0:00:02
解决方案
通过以下设置,我成功地 运行 两个客户端,三个 API´s,一个 Wordpress 和 Php 分析网站没有冲突:例如 API's 将 Apache2 HTTP 服务器作为代理(侦听端口 80)转发到 Nginx(不受 Supervisor 控制,这实际上是解决方案!)Gunicorn 由 Supervisor 控制。所以我就不再对 Nginx 进程使用 Supervisor 了。它可能在不使用 ipv6only 的情况下工作,但我想使用它是为了更好地控制 IP 绑定。所以我对此没意见。