nginx error_log 报告“bind() to 0.0.0.0:80 failed (48: Address already in use)”

JCo*_*obb 9 nginx mac-osx

我最近在 OS X 10.9 Mavericks 上通过 MacPorts 安装了 nginx 和 PHP-FPM,虽然它可以工作,但我的主要 error_log 一直说端口 80 正在使用中。

2013/10/25 11:27:36 [emerg] 4510#0: bind() to 0.0.0.0:80 failed (48: Address already in use)
2013/10/25 11:27:36 [notice] 4510#0: try again to bind() after 500ms
2013/10/25 11:27:36 [emerg] 4510#0: bind() to 0.0.0.0:80 failed (48: Address already in use)
2013/10/25 11:27:36 [notice] 4510#0: try again to bind() after 500ms
2013/10/25 11:27:36 [emerg] 4510#0: bind() to 0.0.0.0:80 failed (48: Address already in use)
2013/10/25 11:27:36 [notice] 4510#0: try again to bind() after 500ms
2013/10/25 11:27:36 [emerg] 4510#0: bind() to 0.0.0.0:80 failed (48: Address already in use)
2013/10/25 11:27:36 [notice] 4510#0: try again to bind() after 500ms
2013/10/25 11:27:36 [emerg] 4510#0: bind() to 0.0.0.0:80 failed (48: Address already in use)
2013/10/25 11:27:36 [notice] 4510#0: try again to bind() after 500ms
2013/10/25 11:27:36 [emerg] 4510#0: still could not bind()
Run Code Online (Sandbox Code Playgroud)

我没有验证其他任何东西,例如 Apache 正在使用端口 80。

JCo*_*obb 10

当一个解决方案搜索好几个地方,如这一个说,解决方法是删除/注释掉listen缺省主机指令行。

#listen       80 default_server;
Run Code Online (Sandbox Code Playgroud)

这样做对我没有任何改变,主要的 error_log 继续填满。

最后有人在nginx 论坛中解决了类似的问题,建议查看输出

ps ax -o pid,ppid,%cpu,vsz,wchan,command|egrep '(nginx|PID)'
Run Code Online (Sandbox Code Playgroud)

这对我来说是

  PID  PPID  %CPU      VSZ WCHAN  COMMAND
 4963     1   0.0  2504128 -      /opt/local/bin/daemondo --label=nginx --start-cmd /opt/local/sbin/nginx ; --pid=fileauto --pidfile /opt/local/var/run/nginx/nginx.pid
 4967     1   0.0  2475388 -      nginx: master process /opt/local/sbin/nginx
 4969  4967   0.0  2476412 -      nginx: worker process
 5024  1538   0.0  2432784 -      egrep (nginx|PID)
 1969  1874   0.0  2432772 -      tail -F /opt/local/etc/nginx/logs/error.log
Run Code Online (Sandbox Code Playgroud)

我在第一行注意到 pidfile 位置是在 MacPorts 的启动命令中设置的--pidfile /opt/local/var/run/nginx/nginx.pid,它与我在nginx.conf. 我将pid条目改回以匹配 start 命令指定的内容:

pid        /opt/local/var/run/nginx/nginx.pid;
Run Code Online (Sandbox Code Playgroud)

重新启动 nginx 并拖尾 error_log ( tail -F /opt/local/etc/nginx/logs/error.log) 后,我注意到问题已解决。

简而言之:如果您使用的是 MacPorts 版本的 nginx,您可能不想更改 pidfile 的位置。

顺便说一句,如果您查看试图解决此问题的其他页面,特别是通过删除listen指令修复了问题的页面,或者其他类似 Apache 的页面也在使用端口 80,您会注意到这些错误日志说

[emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Run Code Online (Sandbox Code Playgroud)

我有

[emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
Run Code Online (Sandbox Code Playgroud)

我怀疑错误98和错误48之间的区别在于差异,但我无法找到各种错误的任何描述。