Common rule priority issues of Nginx location

Common rule priority issues of Nginx location

Locaiton has four types of matching rules, namely, full match (=), prefix normal match (^~), regular expression match (~ or ~*), normal match

rule

  • The equal sign type (=) has the highest precedence. Once a match is found, no further matches are found.
  • Prefix normal matching (^~) has the second highest priority. Regular expressions are not supported. Use prefix matching. If there are multiple locations matching, the one with the longest expression is used.
  • Regular expression types (~ ~*) have the next highest precedence. Once a match is found, no further matches are found.
  • Regular string matching. If there are multiple location matches, the one with the longest expression is used.

illustrate

  • First determine the exact hit, if it hits, immediately return the result and end the parsing process
  • If it is not finished, determine the common hit of the prefix. If there are multiple hits, use the hit result of the "longest" expression to end the parsing process.
  • If it is not finished, continue to judge the match of the regular expression, according to the order of the regular expression, from top to bottom, once a match is successful, the result is immediately returned and the parsing process ends
  • If it is not finished, continue with the normal hit. The normal hit is similar to the prefix normal hit. The order does not matter. The hit result is determined by the length of the location expression.

Location is used to quickly locate resources and define different ways to process or resolve URL requests, generally: /, = /, ~, ~*, ^~
The priority is: (location = /) > (localtion^~) > (location ~| ~* ) > (location /)
Among them, ~ and ~*, whichever is on top will match the other first.

1. Location / Matching

# "/" is to search for resources directly in the nginx release directory /usr/local/nginx/html/, such as location.html
location / {
        root html;
        index index.html index.htm;
    }

Create a location.html file in the release directory with the content: this is location.html.
When executing 172.16.0.9/location.html, the server searches for the location.html file in the publishing directory and returns the result "this is loction.html", as follows:

root@backupserver:/usr/local/nginx/html# ls
50x.html index.html
root@backupserver:/usr/local/nginx/html# echo "this is location.html" > ./location.html
root@backupserver:/usr/local/nginx/html# ls
50x.html index.html location.html
root@backupserver:/usr/local/nginx/html# /usr/local/nginx/sbin/nginx -s reload
root@backupserver:/usr/local/nginx/html# curl 172.16.0.9/location.html
this is location.html
root@backupserver:/usr/local/nginx/html# 

insert image description here

2. Location = / Match

Precise positioning is generally used to match a certain file and has a higher priority than /
for example:
Add a location = / in the nginx configuration file to locate the /data directory. as follows:

server {
        listen 80;
        server_name localhost;
        #charset koi8-r;
        #access_log logs/host.access.log main;
        location / {
            autoindex on;
            root html;
            index index.html index.htm;
        }
        location = /location.html {
            root /data/;
            index index.html;
        }

Restart the Nginx service and test:
1. Don’t create location.html in the /data/ directory yet.
As you can see, a 404 error is reported. This is because = / has a higher priority than /, and the server looks for the location.html file in the second location instead of the first location. Since the directory specified by the second location is /data, there is no location.html file in the /data directory

insert image description here

Create a location.html file in the /data directory

root@backupserver:/usr/local/nginx/html# ls /data/
www
root@backupserver:/usr/local/nginx/html# echo "this is other location.com" > /data/location.html
root@backupserver:/usr/local/nginx/html# ls
50x.html index.html location.html
root@backupserver:/usr/local/nginx/html# curl 172.16.0.9/location.html
this is other location.com
root@backupserver:/usr/local/nginx/html# 

insert image description here

As you can see above, when accessing the server, the server first looks in location = /, even if it is under another location. Exact match has the highest priority. No matter it is above or below the configuration file content, the server will first look for the exact match content.

In addition to exact matches, there are ~, ~*, ^~
~ is case sensitive, and matches are strictly case sensitive
~* is case insensitive and does not distinguish between upper and lower case.
^~ is used to match the beginning of uri. After the match is successful, the search for subsequent regular expression matches will stop.
The highest priority among the above is exact match. location = /, followed by: ^, and then ~*. These two are based on the configuration file content, and they will be matched first. The lowest priority is /

The above rules are widely used when using nginx, for example, when multiple servers are used to separate static and dynamic websites:

location ~ .*\.(html|htm|js|css|txt|png|jpg|jpeg|doc)$ {
root html;
}

This is the end of this article about the common rule priorities of Nginx location. For more relevant content about Nginx location rule priorities, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • In-depth explanation of nginx location priority
  • A brief analysis of the matching priority of Nginx configuration location
  • Nginx configuration directive location matcher priority and security issues

<<:  The difference between div and span in HTML (commonalities and differences)

>>:  MySQL Series 8 MySQL Server Variables

Recommend

Mysql query the most recent record of the sql statement (optimization)

The worst option is to sort the results by time a...

Summary of 7 reasons why Docker is not suitable for deploying databases

Docker has been very popular in the past two year...

mysql error number 1129 solution

SQLyog connects to mysql error number 1129: mysql...

Four completely different experiences in Apple Watch interaction design revealed

Today is still a case of Watch app design. I love...

Analysis of Mysql data migration methods and tools

This article mainly introduces the analysis of My...

Summary of Mysql common benchmark commands

mysqlslap Common parameter description –auto-gene...

HTML 5 Preview

<br />Original: http://www.alistapart.com/ar...

Some common properties of CSS

CSS background: background:#00ffee; //Set the back...

Mysql5.6.36 script compilation, installation and initialization tutorial

Overview This article is a script for automatical...

MySQL 5.7.17 winx64 installation and configuration graphic tutorial

I summarized the previous notes on installing MyS...

25 Ways and Tips to Increase Web Page Loading Speed

Introduction <br />Not everyone has access t...

Detailed explanation of JavaScript axios installation and packaging case

1. Download the axios plugin cnpm install axios -...