nginx refuses a location block with "directive is not allowed here"
nginx: [emerg] "location" directive is not allowed here in /etc/nginx/nginx.conf:76Short answer
nginx only accepts location inside a server block (or inside another location). The error names the file and line of a location that sits somewhere else: straight in http, after a stray closing brace, or in a file included at the http level. Move it into a server block and run nginx -t.
Fix it fast
1Find out which cause you have
Print the lines around the one in the error, using the file name and number it gives. Then look for the block that encloses it.
sudo sed -n '60,90p' /etc/nginx/nginx.conf2Match what you see, then jump to its fix
- 1
the reported line is inside http { } but not inside a server { }The location sits directly in the http block, outside any serverFix - 2
the error line is a location that looks correctly placed in the fileAn extra closing brace ended the server block earlyFix - 3
the error names a file under conf.d or sites-enabled, not nginx.confAn included file holds bare location blocks but is included at the http levelFix
- 1
What does the error look like?
nginx: [emerg] "location" directive is not allowed here in /etc/nginx/nginx.conf:76nginx: configuration file /etc/nginx/nginx.conf test failed
Cause 1 of 3
The location sits directly in the http block, outside any server
1Check
location belongs to the server context. In the config structure server lives in http, and location lives in server, so a location written next to a server block is rejected.
sudo nginx -T 2>/dev/null | grep -n -E "^\s*(http|server|location)\b"2Fix
Wrap the location in a server block, or move it into the server block that should own it.
http {
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
}
}
}3Verify
The test reports a valid configuration.
sudo nginx -tConfirmed by nginx.org
Cause 2 of 3
An extra closing brace ended the server block early
1Check
One } too many closes the server before its last locations, so they end up outside it. Count the braces; the totals must match.
grep -o '{' /etc/nginx/nginx.conf | wc -l
grep -o '}' /etc/nginx/nginx.conf | wc -l2Fix
Find the first line where the nesting closes too soon, remove the extra brace, and keep each location before the server's final }.
server {
location /api/ { proxy_pass http://127.0.0.1:3000; }
location / { root /var/www/html; }
} # the only closing brace for this server3Verify
Test again; the line number in any new error will have moved or disappeared.
sudo nginx -tConfirmed by nginx.org
Cause 3 of 3
An included file holds bare location blocks but is included at the http level
1Check
include pastes the file where the include line stands. If that line is inside http, the file must contain whole server blocks. Find where the file is pulled in.
sudo grep -rn "include" /etc/nginx/nginx.conf2Fix
Either wrap the file's contents in a server block, or include it from inside the server block that should use it.
server {
listen 80;
server_name example.com;
include /etc/nginx/snippets/app-locations.conf; # contains only location blocks
}3Verify
Test the configuration and reload.
sudo nginx -t && sudo systemctl reload nginxConfirmed by nginx.org
How do I stop it coming back?
- Indent every block by one level so a misplaced location is visible at a glance.
- Keep one server block per file in conf.d and put only location blocks in snippet files.
- Run nginx -t after every edit before you reload.
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 nginx location block answers 404 although the file exists
open() "/var/www/site/img/logo.png" failed (2: No such file or directory)
nginx builds the file path from the request URI, so a 404 usually means the path it built is not the path you expected.
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