Nginx Error – bind() to 0.0.0.0:443 Failed (48: Address Already in Use)

mac-osxnginx

I have Laravel Valet 2.1.6 installed on Mac OS 10.14.2.

nginx 1.15.8 is installed using brew.

I restarted my Mac without installing any updates or new software, and now all example.test sites are giving a 502 error with the following showing in the /usr/local/var/log/nginx/error.log log:

2019/01/17 20:38:47 [warn] 31277#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /usr/local/etc/nginx/nginx.conf:1
2019/01/17 20:38:47 [emerg] 31277#0: bind() to 0.0.0.0:443 failed (48: Address already in use)
2019/01/17 20:38:47 [emerg] 31277#0: bind() to 0.0.0.0:443 failed (48: Address already in use)
2019/01/17 20:38:47 [emerg] 31277#0: bind() to 0.0.0.0:443 failed (48: Address already in use)
2019/01/17 20:38:47 [emerg] 31277#0: bind() to 0.0.0.0:443 failed (48: Address already in use)
2019/01/17 20:38:47 [emerg] 31277#0: bind() to 0.0.0.0:443 failed (48: Address already in use)
2019/01/17 20:38:47 [emerg] 31277#0: still could not bind()

At the same time, I get the following in the /Users/Myself/.config/valet/Log/nginx-error.log log:

2019/01/17 20:41:34 [error] 32071#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: example.test, request: "GET / HTTP/2.0", upstream: "fastcgi://unix:/Users/Myself/.config/valet/valet.sock:", host: "example.test"

When I run ps ax -o pid,ppid,%cpu,vsz,wchan,command|egrep '(nginx|PID)' I see this list:

  PID  PPID  %CPU      VSZ WCHAN  COMMAND
32064     1   0.0  4306660 -      nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;
32065 32064   0.0  4333284 -      nginx: worker process
32066 32064   0.0  4332260 -      nginx: worker process
32067 32064   0.0  4333284 -      nginx: worker process
32068 32064   0.0  4333284 -      nginx: worker process
32069 32064   0.0  4326116 -      nginx: worker process
32070 32064   0.0  4316900 -      nginx: worker process
32071 32064   0.0  4368236 -      nginx: worker process
32072 32064   0.0  4331236 -      nginx: worker process
32073 32064   0.0  4326116 -      nginx: worker process
32074 32064   0.0  4340452 -      nginx: worker process
32075 32064   0.0  4333284 -      nginx: worker process
32076 32064   0.0  4334308 -      nginx: worker process
36815  1406   0.0  4268060 -      egrep (nginx|PID)

None of the following solves the issue:

  • sudo killall nginx
  • brew services restart nginx
  • brew services restart php
  • valet restart
  • Restarting my Mac
  • valet uninstall && valet install then valet park on the relevant dir

Apache is not running as a conflicting service.

I tried doing sudo /usr/local/opt/nginx/bin/nginx -g 'daemon off;' and got this:

nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:60 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:60 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:60 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:60 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:60 failed (48: Address already in use)
nginx: [emerg] still could not bind()

Running sudo lsof -i tcp:80 produces:

COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
nginx   42220 root    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42221 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42222 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42223 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42224 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42225 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42226 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42227 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42228 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42229 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42230 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42231 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42232 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)

Basically the same output when I run that command for port 443.

This valet issue post suggests valet domain test might fix it, but that didn't help.

Tried reinstalling PHP but no luck:

  • brew uninstall –force php
  • brew cleanup
  • brew install php
  • valet uninstall && valet install

Tried deleting all valet config/certificate directories, uninstalled valet again, then reinstalled and re-parked my project dir.

Tried running nginx as root:

  • sudo brew services stop nginx
  • Confirmed nothing is running on port 80 and no nginx processes
  • sudo brew services start nginx

Best Answer

I'm guessing the real reason for this is that you ran the brew services as root in a prior installation.

sudo brew services restart nginx

And you ended up putting a LaunchAgent inside /Library/LaunchAgents and that is starting up nginx as root at startup giving you 2 instances. You can check for this in activity monitor.

Go into /Library/LaunchAgents and remove the offending Agent, and restart.

Related Topic