nginx says the PID file is invalid, or systemd cannot read it
invalid PID number "" in "/run/nginx.pid"Short answer
nginx and systemd both read one small file that holds the master process ID. If that file is empty, left over from a dead process, or lives at a different path than the other side expects, you get invalid PID number or Failed to read PID. Check the file's content, then make nginx.conf and the unit agree on its path.
Fix it fast
1Find out which cause you have
Look at what is inside the PID file and whether a master process is really running. The two answers split the causes below.
sudo cat /run/nginx.pid; echo "---" ps axw -o pid,ppid,command | grep "nginx: master"2Match what you see, then jump to its fix
- 1
invalid PID number "" in "/run/nginx.pid" when you run nginx -s reload or nginx -s quitThe PID file is empty or stale, left behind by a process that diedFix - 2
Failed to read PID from file, or open() of a nginx.pid path that does not existnginx.conf and the service unit point at different PID file pathsFix
- 1
What does the error look like?
invalid PID number "" in "/run/nginx.pid"nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argumentopen() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
Cause 1 of 2
The PID file is empty or stale, left behind by a process that died
1Check
nginx -s reads the file, and it only accepts a number in it. An empty file, or text that is not a number, gives the invalid PID message. If the ps line from the first step shows no master process, the file is a leftover.
sudo od -c /run/nginx.pid | head
pgrep -a nginx2Fix
Stop any master process that is still running with a graceful QUIT signal, remove the leftover file, then start nginx through systemd so it writes a fresh one.
# only if the ps line showed a master: replace 1628 with its PID
sudo kill -s QUIT 1628
sudo rm -f /run/nginx.pid
sudo systemctl start nginx3Verify
The file now holds one number, and a reload signal is accepted without complaint.
cat /run/nginx.pid
sudo nginx -s reloadConfirmed by raw.githubusercontent.com
Cause 2 of 2
nginx.conf and the service unit point at different PID file paths
1Check
The pid directive decides where nginx writes the file; the default is logs/nginx.pid inside the nginx prefix. systemd reads the path from PIDFile= in the unit. If the two differ, systemd looks where nothing was written. nginx -s also uses the pid value of whatever config it loads.
sudo nginx -T 2>/dev/null | grep -E "^\s*pid "
systemctl cat nginx | grep -i pidfile2Fix
Make the pid directive in the main context of nginx.conf equal to PIDFile= in the unit, then restart.
# top of /etc/nginx/nginx.conf, outside http { }
pid /run/nginx.pid;3Verify
Test the configuration, restart through systemd and confirm the unit is active.
sudo nginx -t && sudo systemctl restart nginx
systemctl status nginx --no-pagerConfirmed by freedesktop.org
How do I stop it coming back?
- Start, stop and reload nginx only through systemctl, so a hand-started master never competes with the one systemd tracks.
- Change the pid path in one place, then update PIDFile= to match in the same edit.
- After changing the pid directive, restart nginx once and confirm the new file exists before you rely on reload scripts.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Servers
A Let's Encrypt certificate did not renew with Certbot
Type: connection ... Timeout during connect (likely firewall problem)
Run sudo certbot renew --dry-run and read the Type and Detail lines.
6 causes, each with a fixServers
curl cannot connect to port 80 or 443 on a server running nginx
CURLE_COULDNT_CONNECT (7): Failed to connect() to host or proxy
Exit code 7 means the TCP connection itself failed, before any HTTP happened.
3 causes, each with a fixServers
Django returns a 500 behind Gunicorn and the traceback is nowhere
The traceback shows locally with runserver but not under Gunicorn
With DEBUG set to False, Django's default logging does not print errors to the console.
2 causes, each with a fixServers
Your website on a Google Compute Engine VM times out on port 80
The site works with curl on the VM but not from outside
A timeout, as opposed to a refusal, usually means a firewall dropped the packets.
3 causes, each with a fixServers
Gunicorn rejects a request with "Error parsing headers: limit request headers fields size"
Error parsing headers: 'limit request headers fields'
Gunicorn caps the size of one header field at 8190 bytes and the number of headers at 100 by default.
2 causes, each with a fixServers
HAProxy answers 503 "No server is available to handle this request"
No server is available to handle this request.
HAProxy sends 503 when it has no server to give a request to.
4 causes, each with a fix