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

MySQL msi installation tutorial under windows10 with pictures and text

1. Download 1. Click the latest download from the...

XHTML tags that are easily confused by the location of the use

<br />We have always emphasized semantics in...

RHCE installs Apache and accesses IP with a browser

1. at is configured to write "This is a at t...

Detailed tutorial for installing mysql5.7.21 under Windows

This article shares the installation tutorial of ...

How to open port 8080 on Alibaba Cloud ECS server

For security reasons, Alibaba Cloud Server ECS co...

Vue resets data to its initial state

In some cases, the data in data needs to be reuse...

MySQL partitioning practice through Navicat

MySQL partitioning is helpful for managing very l...

Example of exporting and importing Docker containers

Table of contents Exporting Docker containers Imp...

How to solve the error "ERROR 1045 (28000)" when logging in to MySQL

Today, I logged into the server and prepared to m...

A brief discussion on the mysql execution process and sequence

Table of contents 1:mysql execution process 1.1: ...