The location directive within NGINX server block allows to route request to correct location within the file system. The directive is used to tell NGINX where to look for a resource by including files and folders while matching a location block against an URL. In this tutorial, we will look at NGINX location directives in details.
The NGINX location block can be placed inside a server block or inside another location block with some restrictions. The syntax for constructing a location block is:
location [modifier] [URI] {
...
...
}
The modifier in the location block is optional. Having a modifier in the location block will allow NGINX to treat a URL differently. Few most common modifiers are:
A location can be defined by using a prefix string or by using a regular expression. Case-insensitive regular expressions are specified with preceding “~*” modifier and for a case-insensitive regular expression, the “~” modifier is used. To find a location match for an URI, NGINX first scans the locations that is defined using the prefix strings (without regular expression). Thereafter, the location with regular expressions are checked in order of their declaration in the configuration file. NGINX will run through the following steps to select a location block against a requested URI.
location = /some/path/
and if a match is found then this block is served right away.Let us list few examples of NGINX location blocks using modifier and URI.
In the following example the prefix location / will match all requests but will be used as a last resort if no matches are found.
location / {
...
}
NGINX always tries to match most specific prefix location at first. Therefore, the equal sign in the following location block forces an exact match with the path requested and then stops searching for any more matches.
location = /images {
...
}
The above location block will match with the URL https://domain.com/images
but the URL https://domain.com/images/index.html
or https://domain.com/images/
will not be matched.
The following location block will match any request starting with /images/ but continue with searching for more specific block for the requested URI. Therefore the location block will be selected if NGINX does not find any more specific match.
location /images/ {
...
...
}
The modifier ^~ in the following location block results in a case sensitive regular expression match. Therefore the URI /images or /images/logo.png will be matched but stops searching as soon as a match is found.
location ^~ /images {
...
...
}
The modifier ~* in the next location block matches any request (case-insensitive) ending with png, ico, gif, jpg, jpeg, css or js. However, any requests to the /images/
folder will be served by the previous location block.
location ~* .(png|ico|gif|jpg|jpeg|css|js)$ {
...
...
}
The modifier ~ in the following location block results in a case sensitive regular expression match but doesn’t stop searching for a better match.
location ~ /images {
...
...
}
The modifier ~* in the following location block results in a case insensitive regular expression match but the searching doesn’t stop here for a better match.
location ~* /images {
...
...
}
Understanding NGINX location directive is essential for tracing end points of requested URI in the file system. The modifiers, steps of selecting location block and few examples discussed in this article will help you to get started with location blocks in NGINX easily.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.