Maaz Zindani

Loading the index…

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

nginx refuses a location block with "directive is not allowed here"

Errornginx: [emerg] "location" directive is not allowed here in /etc/nginx/nginx.conf:76

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

  • 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

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

    1. 1the reported line is inside http { } but not inside a server { }The location sits directly in the http block, outside any serverFix
    2. 2the error line is a location that looks correctly placed in the fileAn extra closing brace ended the server block earlyFix
    3. 3the 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

What does the error look like?

  • nginx: [emerg] "location" directive is not allowed here in /etc/nginx/nginx.conf:76
  • nginx: 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 -t

Confirmed by nginx.org

Did this fix it?

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

2Fix

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 server

3Verify

Test again; the line number in any new error will have moved or disappeared.

sudo nginx -t

Confirmed by nginx.org

Did this fix it?

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

2Fix

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 nginx

Confirmed by nginx.org

Did this fix it?

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

Next step

Want this handled for good?

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