The root directive, valid in the http, server, location, and if in location contexts with a default of html, sets the root directory for requests: the file path is built by merely adding the request URI to the root value, so location /i/ { root /data/w3; } sends /data/w3/i/top.gif in response to a /i/top.gif request. The value can contain variables except $document_root and $realpath_root; when the URI has to be modified rather than appended, the alias directive should be used instead. location blocks match a normalized URI — after decoding text encoded in the %XX form, resolving references to relative path components . and .., and compressing adjacent slashes — using a prefix string, a regular expression introduced by ~ for case-sensitive or ~* for case-insensitive matching, or, since 1.31.5, a predicate variable. nginx first selects the longest matching prefix location, then checks regular expressions in the order they appear and stops at the first match, checking predicate locations only when no regular expression matched. Keeping specific prefix locations ahead of broad ones so routing stays predictable is Koskamo editorial guidance. Scope: nginx. Reviewed October 4, 2026.
Serving static files with root and location
Document v1 · published by Koskamo editorial · updated 2026-10-04
Source and provenance
- Original source
- nginx.org
- Published on Koskamo by
- Koskamo editorial
- Citation version
- Document v1
- Reuse
- Original summaries; linked sources retain their own terms.