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

Solution 1:

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.

Solution 2:

I had a quite similar problem with a Homebrew install of Nginx. I had an older Nginx install running as a launchd process even though I had uninstalled it some time before (maybe I didn't lauchctl unload it or Homebrew didn't unlink it on uninstall). Anyway, Nginx wouldn't come up on on brew list nor netstat could find the process using the port. I could only detect it with lsof:

sudo lsof -i 4tcp:8080

The process was running and using the port but I couldn't find it anywhere (I even tried to sudo find / -name nginx -type d to look up the entire disk for directories named nginx, but no success). After some beating the head against the table I thought of checking if the Activity Monitor showed the path to open files for the process, and it does.

Open the Activity Monitor, find the process, double click it and it'll open another window with the process details:

Activity Monitor process details window screenshot

Even though the process and it's files were listed, the files didn't exist on the disk. I just had to force quit the zombie Nginx processes that were running and then the new Nginx installation ran as expected.

I'm not sure, but if I recall it correctly I also tried to ps aux | grep nginx and it didn't show up, but it's worth the shot:

sudo ps aux | grep nginx

Tags:

Nginx

Mac Osx