Detailed explanation of Nginx Location configuration (Location matching order)

Detailed explanation of Nginx Location configuration (Location matching order)

Location means "positioning", which is mainly based on Uri for different positioning. It is essential in the configuration of virtual hosts.

Location can locate different parts of the website to different processing methods.

1. Basic syntax of location

location [=|~|~*|^~] patt {

}

=: Strict match. If the query matches, the search stops and the request is processed immediately.
~: for case-sensitive matching (regular expressions can be used).
~*: for case-insensitive matching (regular expressions can be used).
^~: If you use this prefix with a regular string, it tells nginx not to test the regular expression if the path matches.

2. How location works

3. Simple examples prove the conclusions of the above pictures

Matching process without regular expressions

location = / {
   root /var/www/html/;
   index index.htm index.html;
}

location / {
   root /usr/local/nginx/html;
   index index.html index.htm;
}

The location configuration is as above. If you visit http://xxx.com/, the positioning process is:
1: Exact match hits "/", and the index page is index.htm, so the requested address becomes http://xxx.com/index.htm
2: Match "/index.htm" again. This time the internal redirect URI is already "/index.htm", and the normal match "/" is hit. The root directory is /usr/local/nginx/html
3: The final result is to visit /usr/local/nginx/html/index.htm

Matching process with regular expressions

location / {
  root /usr/local/nginx/html;
  index index.html index.htm;
}

location ~ image {
  root /var/www/;
  index index.html;
}

If we visit http://xx.com/image/logo.png. At this time, the uri is "/image/logo.png", which matches the common match "/" and the regular match "~ image". However, according to the matching process analysis in the above picture, the regular match is finally effective.

So the final access address is: /var/www/image/logo.png. If you want the final matching path to be /var/www/logo.png, you can change the "root /var/www/" in the regular expression to "alias /var/www/"

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of Nginx Location configuration
  • Detailed explanation of nginx location directive
  • Detailed explanation of location configuration in Nginx server
  • Analysis of some basic points of location configuration in Nginx server
  • Nginx basic location syntax and function configuration examples

<<:  The difference and execution method of select count() and select count(1)

>>:  Two ideas for implementing database horizontal segmentation

Recommend

Let's talk about what JavaScript's URL object is

Table of contents Overview Hash Properties Host p...

Windows Server 2016 Quick Start Guide to Deploy Remote Desktop Services

Now 2016 server supports multi-site https service...

20 CSS coding tips to make you more efficient (sorted)

In this article, we would like to share with you ...

express project file directory description and detailed function description

app.js: startup file, or entry file package.json:...

Ubuntu 16.04 image complete installation tutorial under VMware

This article shares with you the installation tut...

Introduction and use of Javascript generator

What is a generator? A generator is some code tha...

Detailed explanation of commonly used CSS styles (layout)

Compatible with new CSS3 properties In CSS3, we c...

HTML code text box limit input text box becomes gray limit text box input

Method 1: Set the readonly attribute to true. INPU...

Why web page encoding uses utf-8 instead of gbk or gb2312?

If you have a choice, you should use UTF-8 In fac...

Uniapp uses Baidu Voice to realize the function of converting recording to text

After three days of encountering various difficul...

Build Tomcat9 cluster through Nginx and realize session sharing

Use Nginx to build Tomcat9 cluster and Redis to r...

mysql5.7.17 installation and configuration example on win2008R2 64-bit system

123WORDPRESS.COM has explained to you the install...

Two methods to implement Mysql remote connection configuration

Two methods to implement Mysql remote connection ...

Summary of MySQL composite indexes

Table of contents 1. Background 2. Understanding ...

js to achieve cool fireworks effect

This article shares the specific code for using j...