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

How to clear default styles and set common styles in CSS

CSS Clear Default Styles The usual clear default ...

MySQL query statement simple operation example

This article uses examples to illustrate the simp...

RGB color table collection

RGB color table color English name RGB 16 colors ...

How to connect JDBC to MySQL 5.7

1. First prepare the MySQL and Eclipse environmen...

iframe multi-layer nesting, unlimited nesting, highly adaptive solution

There are three pages A, B, and C. Page A contains...

MySQL data archiving tool mysql_archiver detailed explanation

Table of contents I. Overview 2. pt-archiver main...

Detailed explanation of VUE's data proxy and events

Table of contents Review of Object.defineProperty...

Regarding the Chinese garbled characters in a href parameter transfer

When href is needed to pass parameters, and the p...

Solution to MySQL connection exception and error 10061

MySQL is a relational database management system ...

How to install MySQL 5.7 from source code in CentOS 7 environment

This article describes how to install MySQL 5.7 f...

Detailed explanation of triangle drawing and clever application examples in CSS

lead Some common triangles on web pages can be dr...

Hide div in HTML Hide table TABLE or DIV content css style

I solved a problem tonight that has been botherin...