EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Nginx Tutorial Nginx Location Directive
Secondary Sidebar
Nginx Tutorial
  • Basic
    • Nginx Add_header
    • Nginx FastCGI
    • Nginx WAF
    • Nginx Forward Proxy
    • Nginx Ingress Annotations
    • Nginx Location Directive
    • Nginx User
    • Nginx Port
    • Nginx Auth_request
    • Nginx Autoindex
    • Nginx if else
    • Nginx uWSGI
    • Nginx X-Forwarded-For
    • Nginx GUI
    • NGINX Unit
    • NGINX if
    • Nginx default_server
    • Nginx worker_connections
    • Nginx Authentication
    • Nginx Gateway Timeout
    • Nginx Error_page
    • Nginx Server Block
    • Nginx Forbidden
    • Nginx Modules
    • Nginx Max Upload Size
    • Nginx vs LiteSpeed
    • Nginx multiple server_name

Nginx Location Directive

Nginx Location Directive

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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.

kl

  • 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.

  1. None – If suppose no modifiers are present in a block of location the requested URI is matched against the URI which was beginning.
  2. Equal to (=) – The sign of equal to is used to match the bock of location which was exactly against the URI which was requested.
  3. Tilde sign (~) – The tilde sign is used in case-sensitive conditions or a regular expression that was matched against the URI which was requested.
  4. 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.
  5. 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

lkhj

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

jhk

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;
}

33

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;
}

jdrt

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;
}

Nginx Location Directive uyi

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;
}

Nginx Location Directive hgt

The below example shows the ~ modifier which was used to exact case-sensitive match from the specified block.

Code –

location ~ /data {
root /data;
}

Nginx Location Directive frt

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 –

  1. Nginx FastCGI
  2. Nginx WAF
  3. Nginx Add_header
  4. laravel nginx
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more