Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

nginx: unexpected end of file, expecting ";" or "}"

Errorunexpected end of file, expecting ";" or "}" in /etc/nginx/sites-enabled/default:20

Short 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.

  • Applies to nginx 1.26, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 -t
  2. 2Match what you see, then jump to its fix

    1. 1The reported line is the last line of the fileA block is missing its closing braceFix
    2. 2nginx points at the line after a directive, or at the closing braceA simple directive is missing its semicolonFix

What does the error look like?

  • unexpected end of file, expecting ";" or "}" in /etc/nginx/sites-enabled/default:20
  • nginx: 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 -l

2Fix

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 -t

Confirmed by nginx.org

Did this fix it?

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 -20

2Fix

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 reload

Confirmed by nginx.org

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.