nginx error_log报告“bind()为0.0.0.0:80失败(48:地址已经在使用)”

我最近通过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() 

我已经证实没有其他的东西,比如Apache正在使用80端口。

当search一个解决scheme的几个地方,比如说这个解决scheme是删除/注释掉默认主机中的listen指令行。

 #listen 80 default_server; 

这样做并没有改变任何东西,主要的error_log继续填满。

最后有人在nginx论坛上对类似的问题进行疑难解答,推荐看一下输出

 ps ax -o pid,ppid,%cpu,vsz,wchan,command|egrep '(nginx|PID)' 

这对我来说是

  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 

我注意到在第一行中,pidfile的位置是在MacPorts的启动命令--pidfile /opt/local/var/run/nginx/nginx.pid设置的,并且与我在nginx.conf指定的位置不同nginx.conf 。 我改变了pid条目,以匹配启动命令指定的内容:

 pid /opt/local/var/run/nginx/nginx.pid; 

重新启动nginx并tail -F /opt/local/etc/nginx/logs/error.log 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) 

和我的

 [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use) 

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

我有一个Nginx的家酿安装非常类似的问题。 我有一个早期的Nginx安装运行作为一个launchd进程,即使我已经卸载了一段时间之前(也许我没有lauchctl unload它或Homebrew没有解除链接卸载)。 无论如何,Nginx不会出现在brew list也不能用netstat来查找使用这个端口的进程。 我只能用lsof来检测它:

 sudo lsof -i 4tcp:8080 

该进程正在运行,并使用端口,但我无法find任何地方(我甚至试图sudo find / -name nginx -type d查找整个磁盘名为nginx目录,但没有成功)。 在对桌面敲打头部之后,我想检查一下活动监视器是否显示了为进程打开文件的path,并且确实如此。

打开活动监视器,find进程,双击它,它会打开另一个窗口的进程的详细信息:

Activity Monitor进程详细信息窗口截图

即使列出了进程及其文件,文件在磁盘上也不存在。 我只是不得不强制退出正在运行的僵尸Nginx进程,然后按预期运行新的Nginx安装。

我不确定,但是如果我正确地记得,我也试过ps aux | grep nginx ps aux | grep nginx并没有显示出来,但值得一试:

 sudo ps aux | grep nginx