Nginx Location
In the configuration syntax for nginx(8), a location is a (collection of) URI(s).
Contents
Prefix Locations
Prefix locations map to directories and files. Like directories, they can be nested.
nginx(8) serves the location with the longest matching prefix.
root /var/www; location / { try_files $uri $uri/ /index.html; } location /static/ { root /usr/local/share/myapp; } location /robots.txt { root /var/www; }
As noted below, regular expression locations will take priority over prefix locations. Try location ^~ /static to override this.
Regular Expression Locations
Regular expression locations are checked in the order they appear in the server block, and the first match is served.
location ~* /images/ { # This operator is case sensitive } location ~* /Images/ { # This operator is case insensitive }
These locations take priority over prefix locations, except for those using the ^~ operator.
Exact Locations
= is a short circuit operator. If a request matches an exact location exactly, nginx(8) immediately serves it. This is mainly useful for the root index.
location = / { try_files $uri $uri/ /index.html; }
Named Locations
Named locations, which are identified by the @ prefix, do not map to directories. Named locations cannot be nested and cannot contain nested locations.
These locations are used for routing.
try_files $uri @uwsgi; location @uwsgi { include /etc/nginx/uwsgi_params; uwsgi_pass unix:///var/www/my-wsgi-app/my-wsgi-app.sock; }
Order of Operations
nginx(8) scans the prefix and exact locations in order of length. If the exact location matches, nginx(8) short circuits and immediately serves that location.
The longest matching location is stored. If that location used the ^~ operator, nginx(8) short circuits and serves that location.
nginx(8) scans the regular expression locations in order of their appearance in the configuration file. If one matches, nginx(8) short circuits and immediately serves that location.
Finally, nginx(8) serves the stored prefix location.
Given the computation needed to process long lists of locations, especially when regular expression locations are present, the short circuit operators can be very important.