Sphere
Log in Join

nginx reload fails with `bind() to 0.0.0.0:443 failed (98: Address already in use)`

We renewed a wildcard certificate and I pushed the new chain to our Ubuntu 22.04 web box running nginx 1.18.0 from apt. The deploy script does nginx -t && nginx -s reload. It printed nothing, exited 0, and the site kept serving the old cert. That was the first clue; a silent reload on this box usually means it sent the signal to the wrong process.

## Reading the error log

The app log was quiet, so I looked at /var/log/nginx/error.log:

2024/09/03 11:22:41 [emerg] 18423#18423: bind() to 0.0.0.0:443 failed (98: Address already in use)
2024/09/03 11:22:41 [emerg] 18423#18423: bind() to [::]:443 failed (98: Address already in use)
2024/09/03 11:22:41 [emerg] 18423#18423: still could not bind()

nginx -s reload had apparently worked, but a new master had tried to bind and failed, then exited. The old master was still running with the old config in memory. That's why the cert didn't change.

nginx -s reload reads /run/nginx.pid, sends HUP to whatever PID is in there, and never checks that the process actually owns the listening socket. It has no way to know a second master exists. The exit code is 0 because, from its point of view, the signal was delivered successfully. Nothing about that failure is visible to the caller.

## Two masters, one pid file

bash
ss -ltnp | grep ':443'
ps -eo pid,ppid,lstart,args | grep '[n]ginx'
cat /run/nginx.pid

There were two master processes. One started at boot under systemd, PID 1120. Another, PID 18423, had been started by hand a few weeks earlier during an incident when someone thought systemd was stuck. That manual one owned the socket. /run/nginx.pid pointed at the systemd one, so nginx -s reload reloaded the process that wasn't listening. Classic.

The reason it got to that state: systemctl reload nginx had failed that morning with a config syntax error, someone "fixed" it by starting nginx manually, and the systemd unit was never told. Two masters can technically coexist if they bind different addresses, but not here.

## The fix

I stopped the stray master gracefully so it released the sockets:

bash
sudo kill -QUIT 18423
sudo nginx -t
sudo systemctl reload nginx

nginx -t came back with the usual two lines:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Then openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -dates showed the new notBefore. Good.

## Keeping it from happening again

The deploy script now calls systemctl reload nginx instead of nginx -s reload, so it goes through the unit and the same pid file systemd manages. I also added a check that fails if more than one nginx master is running:

bash
[ "$(pgrep -c -f 'nginx: master process')" -eq 1 ] || exit 1

Cheap, and it would have caught this weeks earlier. I still don't know who started the second master or why nobody noticed the duplicate in monitoring; the graph only ever showed one set of worker processes because they were all called nginx.