A brief analysis of the matching priority of Nginx configuration location

A brief analysis of the matching priority of Nginx configuration location

Preface

The location in the server block in the Nginx configuration is used to match the request URI so that different URIs can be processed differently.

location type and conditions for successful matching

  • location = expression {} Exact match, only when URI and expression are exactly the same, the match is successful;
  • location expression {} Normal matching, as long as the characters in the front part of the URI are the same as expression, the match is successful;
  • location ^~ expression {} Normal matching, as long as the first part of the URI is the same as expression, the match is successful;
  • location ~ regex {} regular matching (case sensitive);
  • location ~* regex {} regular expression matching (case insensitive);

The summary can be divided into three categories: exact matching, ordinary matching and regular matching.

Matching logic and priority

For a request entering Nginx, its URI may meet the matching conditions of multiple locations, but the request will ultimately be processed by only one location logic (redirection is not counted), so there must be a priority difference when matching. Nginx's matching logic is as follows:

1. Perform an exact match

If the request URI matches location = (meets the exact match condition), the content of the location is executed and no other matches are performed. Otherwise, see the next item.

2. Perform normal matching

Next, Nginx will compare the URI with all the normal matching locations (regardless of ^~ type or unsigned type), and then find the location with the highest matching degree. If the location is of ^~ type, it will stop matching and execute the content of the location. If the location with the highest matching degree is of unsigned type, it will keep this unsigned location and continue with the following regular matching. If the URI does not meet the conditions of any normal matching location, it will go directly to the next step.

3. Perform regular expression matching

The matching of regular expressions is related to the order of location. Nginx will match from top to bottom. If a location is matched, the logic of that location will be executed, and all subsequent regular expression locations will be ignored, terminating the matching. If no regular expression is matched, the previously reserved unsigned location will be executed. If no location was reserved before, 404 will be returned directly.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

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

<<:  How to implement two-way binding function in vue.js with pure JS

>>:  Explanation of the usage of replace and replace into in MySQL

Recommend

Why the explain command may modify MySQL data

If someone asked you whether running EXPLAIN on a...

Detailed explanation of MySQL combined query

Using UNION Most SQL queries consist of a single ...

Web Design Principles of Hyperlinks

<br />Related articles: 9 practical tips for...

A brief discussion on the principle of shallow entry and deep exit of MySQL

Table of contents 1. Overview of the page 2. Infi...

Sample code using vue-router in html

Introducing vue and vue-router <script src=&qu...

How to use Nginx to prevent IP addresses from being maliciously resolved

Purpose of using Nginx Using Alibaba Cloud ECS cl...

Solution to MySQL startup successfully but not listening to the port

Problem Description MySQL is started successfully...

MYSQL performance analyzer EXPLAIN usage example analysis

This article uses an example to illustrate the us...

Teach you the detailed process of installing DOClever with Docker Compose

Table of contents 1. What is Docker Compose and h...

Implementing a web player with JavaScript

Today I will share with you how to write a player...

4 ways to modify MySQL root password (summary)

Method 1: Use the SET PASSWORD command First log ...

Mysql sql slow query monitoring script code example

1. Modify my.cnf #The overall effect is that both...