In-depth explanation of nginx location priority

In-depth explanation of nginx location priority

location expression type

~ indicates to perform a regular match, case-sensitive
~* means to perform a regular match, which is case insensitive
^~ means ordinary character matching. Use prefix matching. If the match is successful, no other locations will be matched.
= Performs an exact match of common characters. That is, an exact match.
@ "@" defines a named location, used for internal targeting, such as error_page, try_files

Location priority description

The order of locations in nginx's location and configuration does not matter much. The type of the location expression. For expressions of the same type, the one with a longer string will be matched first.

Here are the instructions in order of priority:

First priority: The equal sign type (=) has the highest priority. Once a match is found, no further matches are searched.
Second priority: ^~ type expressions. Once a match is found, no further matches are searched.
Third priority: Regular expression type (~ ~*) has the second highest priority. If there are multiple locations that match the regular expression, the one with the longest regular expression is used.
Fourth priority: regular string matching type. Match by prefix.

The location priority example configuration items are as follows:

location = / {
# Only matches requests /
[ configuration A ]
}

location / {
# Matches all requests starting with /. But if there is a longer expression of the same type, the longer expression is chosen. If there is a regular expression that can be matched, # will match the regular expression first.
[ configuration B ]
}

location /documents/ {
# Matches all requests starting with /documents/. But if there is a longer expression of the same type, the longer expression is chosen.
#If there is a regular expression that can be matched, the regular expression will be matched first.
[ configuration C ]
}

location ^~ /images/ {
# Match all expressions starting with /images/. If the match is successful, stop matching and searching. Therefore, even if there is a matching regular expression location, it will not be used [configuration D]
}

location ~* \.(gif|jpg|jpeg)$ {
# Match all requests ending with gif jpg jpeg. However, requests starting with /images/ will use Configuration D
[ configuration E ]
}

Request matching example

/ -> configuration A
/index.html -> configuration B
/documents/document.html -> configuration C
/images/1.gif -> configuration D
/documents/1.jpg -> configuration E

Note that the above matching has nothing to do with the order in which they are defined in the configuration file.

Summarize

This is the end of this article about nginx location priority. For more information about nginx location priority, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

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

<<:  Implementation process of row_number in MySQL

>>:  How to use indexes to optimize MySQL ORDER BY statements

Recommend

Tools to convert static websites into RSS

<br /> This article is translated from allwe...

How to use CSS style to vertically center the font in the table

The method of using CSS style to vertically cente...

Differences and usage examples of for, for...in, for...of and forEach in JS

for loop Basic syntax format: for(initialize vari...

When to use table and when to use CSS (experience sharing)

The main text page of TW used to have a width of 8...

Use Grafana+Prometheus to monitor MySQL service performance

Prometheus (also called Prometheus) official webs...

HTML form_PowerNode Java Academy

1. Form 1. The role of the form HTML forms are us...

Bootstrap 3.0 learning notes button style

This article mainly explains the style of buttons...

Tutorial on installing Microsoft TrueType fonts on Ubuntu-based distributions

If you open some Microsoft documents with LibreOf...

Example code for CSS columns to achieve two-end alignment layout

1. Going around in circles After going around in ...

CentOS 6.4 MySQL 5.7.18 installation and configuration method graphic tutorial

The specific steps of installing mysql5.7.18 unde...

Ubuntu installs multiple versions of CUDA and switches at any time

I will not introduce what CUDA is, but will direc...

Steps to set up HTTPS website based on Nginx

Table of contents Preface: Encryption algorithm: ...

Introducing the code checking tool stylelint to share practical experience

Table of contents Preface text 1. Install styleli...