Nginx root vs alias: Map URLs to Files
Two ways to find a file
A URL describes what a client requests; a filesystem path describes where a file lives on the server. With root, Nginx adds the processed URI to a base directory. With a normal prefix alias, it substitutes a different directory for the matched prefix. These snippets belong inside an HTTP server block. Relative filesystem paths in these examples use the private Nginx lab prefix, not your shell’s current directory by assumption.
For /images/logo.svg, the candidate is site/images/logo.svg. The name images is already part of the URI; do not add it a second time to the root. A file must actually exist and be readable by the worker before a successful response is possible.
For /pictures/logo.svg, the prefix /pictures/ is replaced by shared/, leaving shared/logo.svg. Both approaches can serve the same physical asset under different public URLs. Choose root when the directory tree mirrors the URL; choose alias when it does not.
Keep the slash boundary consistent
request: /images/logo.svg?size=smallroot site -> site/images/logo.svgrequest: /pictures/logo.svgalias shared/ at /pictures/ -> shared/logo.svgThe query string does not name a directory. A slash-terminated location and slash-terminated directory alias make the substitution easy to reason about. A missing slash can concatenate names instead of producing the intended file. A prefix without a slash boundary may match unrelated paths; avoid that configuration. Only point a mapping at a dedicated public directory, never at the parent of private files.
Regex aliases need a deliberate filename
location ~ ^/badges/([a-z]+)[.]svg$ { alias shared/$1.svg;}The parentheses capture lowercase letters; $1 inserts those letters into a complete path. /badges/leaf.svg maps to shared/leaf.svg. This deliberately narrow pattern is not a general upload or file-download authorization policy. Do not interpolate unconstrained user-supplied paths or use a permissive catch-all capture for private files. The regex location and prefix alias are different mechanisms.
Check your mapping
curl --noproxy '*' --max-time 2 http://127.0.0.1:18089/images/logo.svgcurl --noproxy '*' --max-time 2 http://127.0.0.1:18089/pictures/leaf.svgBoth should return SVG files with status 200. If one fails, verify the selected location, the computed file path, file existence and directory traversal permissions. Do not weaken permissions globally. In the next lesson you will control what happens when the calculated path does not exist.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Root and alias path calculations
Predict the file or response and explain the selected behavior.
With root site, which path is checked for /images/logo.svg?
With location /pictures/ and alias shared/, which path serves /pictures/leaf.svg?
Why pair /pictures/ with shared/?
For /images/logo.svg?size=small, what does the query change about the static root path?
Why use a narrow capture in a regex alias?