Nginx Location directive URI matching rules detailed summary

Nginx Location directive URI matching rules detailed summary

1. Introduction

The location instruction is the core configuration of the http module. It receives requests sent by users according to pre-defined URL matching rules. According to the matching results, it forwards the requests to the background server, directly rejects illegal requests and returns 403, 404, 500 error processing, etc.

2. Location command syntax

location [=|~|~*|^~|@] /uri/ { … } or location @name { … }

3. URI matching pattern

The location directive is divided into two matching modes:
1> Ordinary string matching: rules starting with = or without a leading character (~) at the beginning
2> Regular expression matching: Starting with ~ or ~* indicates regular expression matching, ~* indicates that the regular expression is case-insensitive

4. Location URI matching rules

When nginx receives a request, it intercepts the URI part of the request and searches for URI matching patterns defined in all location instructions. In the server module, you can define multiple location instructions to match different URL requests and URI matching patterns for different location configurations. The overall matching principle is: match the common string pattern first, then match the regular pattern. Only recognize the URI part, for example, the request is: /test/abc/user.do?name=xxxx

When a request comes in, the process of Nginx matching this request is as follows:

1> First check whether there is an exact match starting with =, such as: location = /test/abc/user.do { … }

2> Search for a normal match again, based on the principle of the largest prefix. If there are the following two locations, the latter one will be matched
* location /test/ { … }
* location /test/abc { … }

3> After matching a common pattern, the search does not end, but the current matching result is temporarily stored and the search for the regular matching pattern continues

4> After all regular matching patterns find the first matching item in location, this item will be used as the final matching result. Therefore, the matching rules of regular matching items are affected by the order of definition, but ordinary matching patterns are not.

5> If no regular match is found, the cached result in step 3 is used as the final match result
6> If no match is found, return 404

5. Differences between exact matching and fuzzy matching
The difference between location =/ { … } and location / { … } is:
* The first one is an exact match, which only responds to / requests. All requests like /xxx or /xxx/xxxx will not match it as a prefix.
* The latter means that any request prefixed with / will be matched. For example: /abc , /test/abc , /test/abc/aaaa

6. Regular and irregular matching

1> location ~ /test/.+.jsp$ { … }: Regular matching, supporting standard regular expression syntax.
2> location ^~ / { … }: ^~ means turning off regular matching. When this common matching pattern is found, the search for regular matching patterns will no longer continue.

...
http {
  ...
  server {
    listen 80;
    server_name localhost;

    location / {
      root html;
      index index.html index.htm;
      # deny all; reject the request and return 403
      # allow all; allow request}

    location /abc {
      deny all;
    }

    location ~ /.+\.jsp$ {
      proxy_pass http://location:9090;
    }

    # Match all jsp files in the /test path location ~ /test/.+\.jsp$ {
      proxy_pass http://localhost:8080;
    }

    # Define various error pages error_page 404 /404.html

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
      root html;
    }

    # @Similar to variable definition# error_page 403 http://blog.csdn.net; #This definition is not allowed, and it is required to use @ to define temporary variables to implement error_page 403 @page403;
    location @page403 {
      proxy_pass http://blog.csdn.net;
    } 
  }
}

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 matching rules
  • Nginx rewrite regular matching rewriting method example
  • Nginx location matching rule example
  • How to match multiple conditions using nginx
  • Implementation of Nginx forwarding matching rules
  • A brief summary of nginx matching rules (recommended)
  • Detailed explanation of Nginx's various matching methods

<<:  MySQL 5.7.20 Green Edition Installation Detailed Graphic Tutorial

>>:  Vue implements the right slide-out layer animation

Recommend

Detailed explanation of the usage of grep command in Linux

1. Official Introduction grep is a commonly used ...

Detailed explanation of Javascript closures and applications

Table of contents Preface 1. What is a closure? 1...

How to install mysql5.6 in docker under ubuntu

1. Install mysql5.6 docker run mysql:5.6 Wait unt...

Use Smart CSS to apply styles based on the user's scroll position

By adding the current scroll offset to the attrib...

Causes and solutions for MySQL too many connections error

Table of contents Brief summary At noon today, th...

Detailed explanation of Docker Volume permission management

Volume data volume is an important concept of Doc...

Javascript to achieve drumming effect

This article shares the specific code of Javascri...

Why node.js is not suitable for large projects

Table of contents Preface 1. Application componen...

HTML is something that web page creators must learn and master.

What are the benefits of learning HTML? 1: Easily...

Solution to Chinese garbled characters when operating MySQL database in CMD

I searched on Baidu. . Some people say to use the...

Detailed explanation of the use of Docker commit

Sometimes you need to install certain dependencie...

Detailed explanation of the implementation of nginx process lock

Table of contents 1. The role of nginx process lo...

HTML basic summary recommendation (title)

HTML: Title Heading is defined by tags such as &l...

MySQL Database Basics SQL Window Function Example Analysis Tutorial

Table of contents Introduction Introduction Aggre...