Updated March 28, 2023
Definition of Nginx Location Directive
Nginx location directive with the server block of nginx will allow us to route the request for correcting location from the file system. The nginx location directive is used to tell the nginx where to look for the resources including folders and files, at the time of matching URI against the block. Location directive block will be placed inside into the block of the server or inside into another block.
What is nginx location directive?
It is very useful and important at the time of working with nginx. Basically, the location directive is located in the block of the server. Nginx is separating configuration into the blocks of the server, which is useful for working in a hierarchical fashion. When a request is made same time the web server will begin its process of determining which block of configuration we have used to server the request of the client. We are using two main blocks in the nginx location directive configuration files. i.e. location blocks and server blocks. A server block is consisting a subset of configuration which defines a virtual server.
How to choose Nginx location directive?
We can define the nginx location directive by using the string of prefixes or by using the regular expression which was specified and preceding with ~* modifier and it is a regular expression that was case sensitive. For finding a location match for the URI, the nginx location directive first scanned location which was defined by using the directive of location. After the regular expressions are checked as per order declaration into the file of configuration.
Nginx is running through the following steps for selecting the block of location against to the URI which was requested.
- In the first step, the nginx will start for looking an exact match that was specified with the location = /path and suppose if the match is found then this block is used at the same time.
- If suppose we have not found any exact location blocks then nginx will proceed to match the longest prefixes which were non-exact, and if suppose match is found where ^~ modifier is used and then nginx is stop searching further and then this block will be selected for serving the request.
- If the longest prefixes which were matched location were not contained ^~ this modifier then it will store the match temporarily and it will proceed for the following steps as follows.
- In this step now nginx will shift the searching to the specified block of location which is containing ~ and ~* modifiers to select the block of the first location which matches the requested URI and which was immediately selected for serving those requests.
- If suppose we have not found any locations in the above steps which was matched against the URI which was requested then the prefix location which was previously stored is used for serving the request.
For each request, the nginx will go through the process of choosing the location which was best. The block was used for serving the request. Nginx location directive is doing this by request comparing against a block of each location which was used to setup up the configuration.
Nginx location directive syntax
The location directive syntax contains modifiers and URI.
1.
location [modifier] [URI] {
……
}
2.
location modifier (optional) location_match {
…
}
The modifier into the block of location is an optional parameter. Using the modifier into the block of location is allowing nginx to treat the URI differently. Below is the most common modifier which we are using in the nginx location directive as follows.
- None – If suppose no modifiers are present in a block of location the requested URI is matched against the URI which was beginning.
- Equal to (=) – The sign of equal to is used to match the bock of location which was exactly against the URI which was requested.
- Tilde sign (~) – The tilde sign is used in case-sensitive conditions or a regular expression that was matched against the URI which was requested.
- Tilde followed by an asterisk (~*) – The sign of tilde which was followed by an asterisk is used in regular expression which was case sensitive and was a match against the URI which was requested.
- Carat followed by tilde (^~) – The sign of carat followed by tilde was used to perform the non-regular expression which was longest against the requested URI.
Nginx location directive Examples
To use the nginx location directive we need to install nginx in our system.
Below we are installing the nginx server on the ubuntu system. We are installing the nginx server by using the following apt-get command as follows.
apt-get install nginx
After installing the nginx server we are checking the nginx installed version by using the following command as follows. We can check the nginx version as well as the running status of the nginx server.
nginx –V
The below example shows nginx is matching all the requests but it will be used at the resort which was last is supposed we have not found any match.
Code –
location / {
root /nginx_data;
}
The below example shows match exact nginx location by using the URL as follows. Nginx is always matching most specific locations.
Code –
location /data/ {
root /data;
}
The below example shows the block directory nginx location as follows. In below example match any request which was starting from data.
Code –
location /data/ {
root /data;
}
The below example shows nginx regular expression example as follows. The ^~ modifier is used in the following block of results.
Code –
location ^~ /data {
root /data;
}
The below example shows the ~ modifier which was used to exact case-sensitive match from the specified block.
Code –
location ~ /data {
root /data;
}
Conclusion
Nginx location directive is very useful and important at the time of working with nginx. Basically, the nginx location directive is located in the block of the server. Nginx location directive with the server block of nginx will allow us to route the request for correcting location from the file system.
Recommended Articles
This has been a guide to Nginx Location Directive. Here we discussed the Definition, What is nginx location directive, and examples with code implementation. You can also go through our other suggested articles to learn more –