nginx – Error Log Reports ‘bind() to 0.0.0.0:80 Failed (48: Address Already in Use)’

mac-osxnginx

I recently installed nginx and PHP-FPM via MacPorts on OS X 10.9 Mavericks and although it works my main error_log continuously says port 80 is in use.

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()

I have verified nothing else such as Apache is using port 80.

Best Answer

When searching for a solution several places such as this one say the solution is to remove/comment out the listen directive line in the default host.

#listen       80 default_server;

Doing so didn’t change anything for me, and the main error_log continued to fill up.

Finally someone in the nginx forums troubleshooting a similar problem recommended looking at the output of

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

Which for me was

  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

I noticed in the first line that the pidfile location was being set in MacPorts’ startup command --pidfile /opt/local/var/run/nginx/nginx.pid and that it was different than the location I had specified in my nginx.conf. I changed the pid entry back to match what the start command was specifying:

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

After restarting nginx and tailing the error_log (tail -F /opt/local/etc/nginx/logs/error.log) I noticed the problem was fixed.

In short: If you’re using the MacPorts version of nginx you probably don’t want to mess with changing the location of the pidfile.

As an aside, if you look at other pages trying to solve this issue, specifically ones where the problem was fixed by removing the listen directive or where something else like Apache was also using port 80 you will notice that those error logs say

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

and mine had

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

I suspect the difference between error 98 and error 48 is the difference but I wasn't able to find any description of the various errors.