nginx location precedence

NGINX Location Blocks Precedence

Are you wondering “In what order do NGINX location blocks work?” If so then this article will help you. In this article, we will look at the location blocks precedence used by NGINX to process various location blocks in your server configuration.


NGINX Location Blocks Precedence

NGINX tries to match the request with each location block, one by one, from top to bottom, in your server configuration file. NGINX supports the following location matching operators

  • = – equal sign is used to match the exact request URI. also known as exact match
  • ~ – tilde sign is used for case sensitive regular expression match
  • ~* – tilde followed by asterisk means case insensitive regular expression match
  • ^~ – carat followed by tilde means non-regular expression match. also known as forward match
  • if no modifier is used, it means the query string should be used as a prefix.

Now let us look at how NGINX matches a requested URI

  1. Directives with = prefix modifer, meaning exact match, take highest precedence. If NGINX finds any location with an exact matct, the searching stops
  2. Next, it looks for forward match (^~). In other words, it will look for locations that begin with the specified request URI. If a match is found, the searching stops.
  3. Next, it looks for regular expression match (~ and ~*), in the order in which they are defined in your configuration file.
  4. If none of the above locations match, NGINX will look for location blocks without any modifiers, meaning location blocks starting with the specified query string. However, in this case, if a match is found, the searching won’t stop. NGINX will continue analyzing further location blocks for a match. Only if no better match is found, then this location block is used.

In other words,

= (exactly)location = /path
^~ (forward match)location ^~ /path
~ (regular expression case sensitive)location ~ /path/
~* (regular expression case insensitive)location ~* .(jpg|png|bmp)
/location /path

Here is another example to help you understand the location priority.

location = / {
    [ configuration A ]
}

location / {
    [ configuration B ]
}

location /data/ {
    [ configuration C ]
}

location ^~ /images/ {
    [ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$ {
    [ configuration E ]
}

The request “/” will match configuration A. Request to “/index.html” will match configuration B. Request “/data/document.html” will match configuration C. Request to “/images/1.gif” will match configuration D, and the “/data/1.jpg” request will match configuration E.

Understanding the location precendence in NGINX will help you configure different location blocks for URL paths.

Also read:

How to Define & Use Variables in Apache
How to Fix Errno 13 Permission Denied in Apache
NGINX : Protect Static Files with Authentication
How to Setup Apache Virtual Host in Windows
How to Escape Percent Sign in URL in Apache

Leave a Reply

Your email address will not be published. Required fields are marked *