Fix nginx unexpected end of file, expecting } (nginx -t)
nginx -t says unexpected end of file, expecting "}" when a { in that file is never closed. The line number is the file's end; find the gap by brace depth.
nginx: [emerg] unexpected end of file, expecting "}" in /etc/nginx/conf.d/default.conf:13A { in the named file was never closed before the file ended. The line number points at the end of the file, not at the missing brace, so the reported line usually looks fine. Trust the file name and locate the gap by counting brace depth.
[1] Run nginx -t to see which file is affected. This reproduction uses a 12-line file with the closing } of location / removed.
# nginx -t2026/10/27 03:36:20 [emerg] 1#1: unexpected end of file, expecting "}" in /etc/nginx/conf.d/default.conf:13nginx: [emerg] unexpected end of file, expecting "}" in /etc/nginx/conf.d/default.conf:13nginx: configuration file /etc/nginx/nginx.conf test failedThe file has 12 lines and the error names line 13: one past the last line, which is the end of file. In the nginx source (ngx_conf_file.c) this message is raised when the parser hits end of file while still inside a block. Each included file is read on its own, so search the file named in the error; there is no need to go through nginx.conf.
[2] Print the brace depth of every line to see where it drifts. Comments after # are ignored.
# awk '{ line=$0; sub(/#.*/, "", line); d += gsub(/{/, "{", line) - gsub(/}/, "}", line); printf "%3d %2d %s\n", NR, d, $0 }' /etc/nginx/conf.d/default.conf 1 1 server { 2 1 listen 80; 3 1 server_name example.com; 4 1 5 2 location / { 6 2 root /usr/share/nginx/html; 7 2 index index.html; 8 2 9 3 location /api/ { 10 3 proxy_pass http://127.0.0.1:8080; 11 2 } 12 1 }If the last line ends at depth 1 instead of 0, one } is missing. Walk upward and look for a line whose indentation disagrees with its depth. Line 9, location /api/, is indented as a direct child of server (depth 2) but sits at depth 3: the location / above it was never closed. Lines with {, } or # inside double quotes can throw the count off, so check those by eye.
[3] Add the missing } and test again. Keep a copy of the original first (to roll back, copy it back into place).
# cp -a /etc/nginx/conf.d/default.conf /root/default.conf.bakPut } under the index line of location /, then test.
# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[4] A slightly different message means a different cause. These came from breaking one thing at a time in the same container.
nginx -t message | Reproduced cause | Line it points to |
|---|---|---|
unexpected end of file, expecting "}" | A { never closed | End of file |
unexpected end of file, expecting ";" or "}" | An opening double quote never closed (return 200 "ok;) | End of file |
unexpected "}" | } without a preceding ; ({ return 204 }) | That line |
unexpected "}" | One extra } at the very end of an include file | The extra } |
"location" directive is not allowed here | An extra } mid-file closes server early | The next location |
unknown directive "4}/" | {4} inside an unquoted regex | That location line |
The confusing one is the extra brace reported as not allowed here rather than unexpected "}". The stray } closes server early, so the following location lands outside any server.
[5] Quote a regex that contains {, } or ;. Without quotes nginx reads { as the start of a block. This is the result of location ~ ^/archive/[0-9]{4}/ { left unquoted:
# nginx -t…nginx: [emerg] unknown directive "4}/" in /etc/nginx/conf.d/default.conf:5nginx: configuration file /etc/nginx/nginx.conf test failed location ~ "^/archive/[0-9]{4}/" { root /usr/share/nginx/html; }The nginx docs say the same for if and rewrite: if a regex includes } or ;, enclose the whole expression in single or double quotes. The quoted version passed nginx -t.
[6] Reload only after the test passes.
# nginx -t && systemctl reload nginxSkipping -t and reloading does not stop a running nginx on the spot. Per the nginx docs, the master process checks the new configuration first and keeps working with the old one if that fails. A restart in that state will fail to start, though, so only leave a file in place once -t passes.
If the syntax passes but a duplicate server_name warning shows up, see conflicting server name ignored. If the error is about overlapping default_server, see duplicate default server.
Analyze your Nginx log in the browser ↗ — nothing is uploaded.