我们的 nginx 服务器自行崩溃,这种情况随机发生了几次。我不知道为什么会发生这种情况。这是我检查时显示的内容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: failed (Result: exit-code) since Thu 2018-11-01 00:48:16 IST; 9h ago
Process: 16654 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCE
Process: 16702 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Process: 16699 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 1353 (code=exited, status=0/SUCCESS)
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.
和 nginx 错误日志
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to [::]:80 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to [::]:443 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to [::]:80 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to [::]:443 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to [::]:80 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to [::]:443 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to [::]:80 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to [::]:443 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to [::]:80 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to [::]:443 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/11/01 00:48:13 [emerg] 16702#16702: still could not bind()
2018/11/01 00:48:16 [alert] 16665#16665: unlink() "/run/nginx.pid" failed (2: No such file or directory)
编辑
sudo netstat -nlp
=====================================================================================================
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1036/mongod
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 30941/mysqld
tcp 0 0 127.0.0.1:587 0.0.0.0:* LISTEN 1382/sendmail: MTA:
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 1147/redis-server 1
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 29550/nginx -g daem
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1153/sshd
tcp 0 0 127.0.0.1:3000 0.0.0.0:* LISTEN 6988/node
tcp 0 0 127.0.0.1:8088 0.0.0.0:* LISTEN 1042/influxd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1382/sendmail: MTA:
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 29550/nginx -g daem
tcp6 0 0 127.0.0.1:7983 :::* LISTEN 1691/java
tcp6 0 0 :::80 :::* LISTEN 29550/nginx -g daem
tcp6 0 0 :::8086 :::* LISTEN 1042/influxd
tcp6 0 0 :::22 :::* LISTEN 1153/sshd
tcp6 0 0 :::8983 :::* LISTEN 1691/java
tcp6 0 0 :::8888 :::* LISTEN 1117/chronograf
tcp6 0 0 :::443 :::* LISTEN 29550/nginx -g daem
udp 0 0 0.0.0.0:68 0.0.0.0:* 825/dhclient
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node PID/Program name Path
unix 2 [ ACC ] STREAM LISTENING 8775 1/init /run/systemd/private
unix 2 [ ACC ] STREAM LISTENING 16995534 25536/systemd /run/user/1000/systemd/private
unix 2 [ ACC ] STREAM LISTENING 17834 1326/systemd /run/user/120/systemd/private
unix 2 [ ACC ] SEQPACKET LISTENING 8779 1/init /run/udev/control
unix 2 [ ACC ] STREAM LISTENING 8790 1/init /run/systemd/journal/stdout
unix 2 [ ACC ] STREAM LISTENING 8793 1/init /run/lvm/lvmpolld.socket
unix 2 [ ACC ] STREAM LISTENING 8794 1/init /run/lvm/lvmetad.socket
unix 2 [ ACC ] STREAM LISTENING 13115 1/init /var/lib/lxd/unix.socket
unix 2 [ ACC ] STREAM LISTENING 17902 1036/mongod /tmp/mongodb-27017.sock
unix 2 [ ACC ] STREAM LISTENING 24330 1053/node /home/ubuntu/.pm2/pub.sock
unix 2 [ ACC ] STREAM LISTENING 17115993 6389/git-credential /home/ubuntu/.git-credential-cache/socket
unix 2 [ ACC ] STREAM LISTENING 24408 1053/node /home/ubuntu/.pm2/rpc.sock
unix 2 [ ACC ] STREAM LISTENING 13112 1/init /run/snapd.socket
unix 2 [ ACC ] STREAM LISTENING 13113 1/init /run/snapd-snap.socket
unix 2 [ ACC ] STREAM LISTENING 13114 1/init /run/acpid.socket
unix 2 [ ACC ] STREAM LISTENING 13118 1/init /var/run/dbus/system_bus_socket
unix 2 [ ACC ] STREAM LISTENING 23857 1114/python /var/run/supervisor.sock.1114
unix 2 [ ACC ] STREAM LISTENING 17939 1382/sendmail: MTA: /var/run/sendmail/mta/smcontrol
unix 2 [ ACC ] STREAM LISTENING 13111 1/init /run/uuidd/request
unix 2 [ ACC ] STREAM LISTENING 13271 1034/iscsid @ISCSIADM_ABSTRACT_NAMESPACE
unix 2 [ ACC ] STREAM LISTENING 18633 1386/php-fpm.conf) /run/php/php7.0-fpm.sock
unix 2 [ ACC ] STREAM LISTENING 9990652 30941/mysqld /var/run/mysqld/mysqld.sock
编辑2
# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
如果发生这种情况,有没有办法自动重新启动 nginx?
答案1
您的netstat
输出显示,当您尝试启动 nginx 时,它已在运行。它是在 systemd 之外手动启动的吗?在这种情况下,通常都是这样。尝试手动终止 nginx 进程,然后在 systemd 中重新启动它。
killall nginx
# and wait until netstat no longer shows it, or use kill -9
systemctl start nginx
也可能是因为 systemd 丢失了正在运行的 nginx 进程的跟踪,因为编写该 systemd 单元的人不知道他们在做什么。它实际上试图使用古老且现已过时的方法start-stop-daemon
向 nginx 发送信号,而 systemd 完全有能力自己完成这件事!这最终肯定会造成麻烦。尝试更新到最新版本的 nginx 和/或 Ubuntu,该服务可能已修复。
或者只需删除错误的ExecStop=
行并将其替换为KillSignal=QUIT
Red Hat nginx systemd 单元所做的操作,以及在 systemd 中执行此操作的正确方法。
答案2
[::]:80
是 ipv6 地址。如果您的 nginx 配置监听端口 80 和端口[::]:80
,则会导致此错误。
我的默认站点可用文件中包含以下内容:
listen 80;
listen [::]:80 default_server;
您可以通过添加 ipv6only=on 来解决[::]:80
这个问题:
listen 80;
listen [::]:80 ipv6only=on default_server;