GrepBoard/Manuals
605 manuals ·

nginx the "alias" directive cannot be used inside the named location

·Tested on nginx 1.28.0 (Docker) · 2026-11·Test scope ↓·4 steps

Fix an alias inside a named nginx location. Map files under a URI prefix, or keep the fallback internal and update the try_files target.

console
$ nginx -t
output
2026/11/23 02:24:51 [emerg] 1#1: the "alias" directive cannot be used inside the named location in /etc/nginx/nginx.conf:5nginx: [emerg] the "alias" directive cannot be used inside the named location in /etc/nginx/nginx.conf:5nginx: configuration file /etc/nginx/nginx.conf test failed

Decide which URI should serve the files. Use a prefix location for a public path. For a file used only as a fallback, give it an internal URI and point the caller there.

[1] Find the block around the flagged directive. Line 5 of the failing configuration contained this block.

/etc/nginx/nginx.conf
        location @fallback { alias /srv/files/; }

The name @fallback identifies an internal jump target. It supplies no URI prefix for alias to replace. nginx rejects the directive before reading its path, so changing file permissions leaves this error in place. nginx 1.28.0 source

[2] Replace the block with a prefix location if clients should fetch files under /assets/. Save the current block and its callers before editing a live configuration.

/etc/nginx/nginx.conf
        location /assets/ { alias /srv/files/; }

For /assets/logo.svg, nginx replaces /assets/ with /srv/files/, giving the path /srv/files/logo.svg. Keep the trailing slash on both directory paths. nginx alias documentation

[3] Check the last argument of any try_files that targets @fallback. Renaming the block alone leaves the old target in place. Use the following pair of locations in the same server if a failed lookup should serve /srv/files/index.html. This is a separate configuration from step 2.

/etc/nginx/nginx.conf
        location / { try_files $uri /_fallback/index.html; }        location /_fallback/ { internal; alias /srv/files/; }

The last argument gives the URI for an internal redirect. With internal, a direct external request for /_fallback/index.html receives a 404. nginx internal documentation

[4] Test the chosen configuration. Both alternatives produced this result independently.

console
$ nginx -t
output
nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful

Check that the original request serves the intended file. If the mapping changed unexpectedly, restore the saved location and caller together and retest. For an error about a URI in proxy_pass, see proxy_pass rules by location type.

Analyze your Nginx log in the browser ↗ — nothing is uploaded.

#nginx#alias#location#try_files