nginx: unexpected end of file, expecting ";" or "}"
unexpected end of file, expecting ";" or "}" in /etc/nginx/sites-enabled/default:20Short answer
nginx reached the end of a config file while a directive or block was still open. Two things cause it: a block that is missing its closing brace, or a last directive that is missing its semicolon. nginx -t names the file and line where it gave up. Count your braces from the top of that file.
Fix it fast
1Find out which cause you have
Run the test. The path and line number at the end of the message are the end of the file nginx could not finish parsing, not always the line with the mistake.
sudo nginx -t2Match what you see, then jump to its fix
What does the error look like?
unexpected end of file, expecting ";" or "}" in /etc/nginx/sites-enabled/default:20nginx: configuration file /etc/nginx/nginx.conf test failed
Cause 1 of 2
A block is missing its closing brace
1Check
Blocks such as server and location end with a set of braces. The docs describe a block as a directive followed by instructions enclosed in { and }. Count the opening and closing braces in the file.
grep -o "{" /etc/nginx/sites-enabled/default | wc -l
grep -o "}" /etc/nginx/sites-enabled/default | wc -l2Fix
Add the missing brace so every location sits inside its server, and every server inside http.
server {
listen 80;
location / {
root /var/www/html;
}
}3Verify
The test now reports syntax is ok.
sudo nginx -tConfirmed by nginx.org
Cause 2 of 2
A simple directive is missing its semicolon
1Check
Simple directives end with a semicolon. A missing one makes nginx read the next lines as parameters of the same directive. Look at the line above the one reported.
sudo nginx -T 2>&1 | grep -n "root\|index\|listen\|server_name" | head -202Fix
End every simple directive with a semicolon.
root /var/www/html;
index index.html index.htm;3Verify
Test and reload together, so nginx only reloads on a clean config.
sudo nginx -t && sudo nginx -s reloadConfirmed by nginx.org
How do I stop it coming back?
- Run nginx -t after every edit, before reloading.
- Copy complete blocks from the docs instead of typing them in pieces.
- Keep one server per file under sites-available and link it, so a broken file is easy to locate.
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 does not start and says the configuration test failed
nginx: [emerg] ... followed by the file name and line number of the faulty directive
The message 'test failed' is only the last line.
3 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 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 fix