nginx does not start and says the configuration test failed
nginx: configuration file /etc/nginx/nginx.conf test failedShort answer
The message 'test failed' is only the last line. The reason is the [emerg] line printed just above it. Run nginx -t as root, read that line, fix the file and line number it names, and test again before starting. The usual reasons are a syntax error, a file nginx cannot open, or testing a different config file.
Fix it fast
1Find out which cause you have
Test the config as root and read the first [emerg] line. It names the file and line to fix. The error log holds the same text.
sudo nginx -t sudo tail -n 20 /var/log/nginx/error.log2Match what you see, then jump to its fix
- 1
[emerg] with a file name and a line number, such as an unexpected end of file or a missing semicolonA syntax error or unknown directive in a config fileFix - 2
[emerg] naming a certificate, include file, or log path, followed by No such file or directoryA file named in the config cannot be openedFix - 3
The test passes for the file you edited, but the service still failsYou tested a different config than the one the service startsFix
- 1
What does the error look like?
nginx: configuration file /etc/nginx/nginx.conf test failednginx: [emerg] ... followed by the file name and line number of the faulty directive
Cause 1 of 3
A syntax error or unknown directive in a config file
1Check
nginx -t checks syntax first. Dump everything that was loaded, including included files, and go to the line it named.
sudo nginx -T | sed -n '1,80p'2Fix
Edit the named file and line. Common finds are a missing semicolon, an unbalanced brace, or a directive in the wrong block. Test again until it prints that the syntax is ok.
sudo nano /etc/nginx/sites-enabled/example.conf
sudo nginx -t3Verify
Start the service and confirm it is active.
sudo systemctl start nginx
systemctl is-active nginxConfirmed by nginx.org
Cause 2 of 3
A file named in the config cannot be opened
1Check
After the syntax check, -t tries to open files that the config refers to. Check the path from the message.
sudo nginx -t 2>&1 | head -n 5
sudo ls -l <path from the message>2Fix
Create the missing file or folder, or remove the directive that points to it. For missing certificates see the page on the certificate file error.
sudo mkdir -p /var/log/nginx3Verify
The test now ends with the 'test is successful' line.
sudo nginx -tConfirmed by nginx.org
Cause 3 of 3
You tested a different config than the one the service starts
1Check
The main file is nginx.conf in /usr/local/nginx/conf, /etc/nginx or /usr/local/etc/nginx, depending on the build, and -c selects another file. A test run with -c does not test the default one.
sudo nginx -t2Fix
Test without -c, so you test the file the service loads, or test the exact file with -c and make the service use that same file.
sudo nginx -t -c /etc/nginx/nginx.conf3Verify
Both the plain test and the service start agree.
sudo nginx -t && sudo systemctl restart nginxConfirmed by nginx.org
How do I stop it coming back?
- Run nginx -t before every reload or restart; a reload with a bad config keeps the old one running.
- Change one thing at a time so the failing line is obvious.
- Keep nginx.conf and site files in version control.
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
nginx will not start because the SSL certificate file is missing
nginx: [emerg] cannot load certificate key "/etc/nginx/ssl/example.com.key": ... No such file or directory
nginx opens every certificate and key file named in the config while it starts, and one of them is not there.
3 causes, each with a fixServers
nginx will not start because port 80 is already taken
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Error 98 means another process already listens on the address and port that nginx wants.
2 causes, each with a fixServers
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 fix