Nginx 不知不觉地停止并出现错误 bind() to 0.0.0.0:80 failed (98: Address already in use)

Nginx 不知不觉地停止并出现错误 bind() to 0.0.0.0:80 failed (98: Address already in use)

我们在演示和生产服务器中运行一个 node js 应用程序,并使用 Nginx 作为反向代理。

Nginx 已在不知情的情况下停止,并出现以下错误。我登录到服务器并手动启动 nginx sudo service nginx start,然后它启动成功。但仍然无法找出关机的根本原因。这种情况在演示服务器中发生了两次,感谢上帝,到目前为止在生产服务器中还没有发生过。

2018/04/05 12:44:03 [notice] 20030#20030: signal process started
2018/04/06 00:35:49 [notice] 23507#23507: signal process started
2018/04/06 12:08:18 [notice] 25473#25473: signal process started
2018/04/06 12:08:18 [error] 25473#25473: open() "/run/nginx.pid" failed (2: No such file or directory)
2018/04/06 12:08:20 [notice] 25479#25479: signal process started
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to [::]:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to [::]:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to [::]:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to [::]:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to [::]:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: still could not bind()
2018/04/06 12:08:24 [alert] 25475#25475: unlink() "/run/nginx.pid" failed (2: No such file or directory)
2018/04/07 00:02:58 [notice] 28369#28369: signal process started
2018/04/07 00:02:58 [error] 28369#28369: invalid PID number "" in "/run/nginx.pid"
2018/04/07 12:12:44 [notice] 30352#30352: signal process started
2018/04/07 12:12:44 [error] 30352#30352: invalid PID number "" in "/run/nginx.pid"
2018/04/08 00:43:19 [notice] 811#811: signal process started
2018/04/08 00:43:19 [error] 811#811: invalid PID number "" in "/run/nginx.pid"

根据问题,它说重新启动时出现问题,但在我的情况下它会自动关闭。

根据此问题,它建议添加ipv6only=on[::]:80,但下面是安装时 nginx 默认文件所具有的内容,并且我已按照以下说明为 node js 添加了 nginx 配置回答

默认服务器配置

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    server_name _;

虚拟主机配置

server {
    listen 80;
    listen [::]:80;
    server_name demo.api.example.com;

需要进一步澄清

我认为20030#20030: signal process started是指 nginx 重新加载。为什么 nginx 每天重新加载两次?这与 Nginx Amplify Agent 有关吗?还是这是 nginx 的默认行为?

服务器详细信息:

  • 操作系统:Ubuntu 16.04.2 LTS
  • Nginx: nginx/1.10.3
  • 附加工具:Nginx Amplify Agent,PM2 2.9.2

答案1

检查是否有服务使用80端口。

netstat -plnt | grep :80

这将向您显示哪个正在 80 端口上运行

答案2

发生这种情况的原因是因为端口 80 当前正在使用,并且很可能是最后一个未正确关闭的 nginx 实例。

您可以在启动 nginx 服务之前检查您的 nginx 配置并进行测试。

它应该在测试时给出成功的输出。

相关内容