introduction
As a high-performance web server and reverse proxy server, Nginx is widely used in load balancing, request forwarding and other scenarios. When configuring Nginx, routing matching rules are a very important concept, which determines how requests are forwarded to different processing logics or backend services. This article will introduce Nginx's routing matching rules and priorities in detail, helping you better understand how to configure routing and how to debug and optimize Nginx configuration.
1. Overview of Nginx's routing matching rules
In Nginx configuration, routing matching is usually implemented through location instructions. Nginx will match the requested URI (Uniform Resource Identifier) with the location block in the configuration, thereby determining how the request is handled.
For example, here is a simple server block containing multiple location blocks:
location / { root /var/www/html;} location /images/ { root /var/www/images;} location /static/ { root /var/www/static;}
- In the above configuration, the location block matches the appropriate processing logic based on the requested URI (eg /images/). Each location block contains a matching rule, and Nginx will check from top to bottom according to the rule priority until a most matching location block is found.
2. Nginx's routing matching rule type
Nginx's location instruction supports several different matching patterns, and different patterns have different matching logic and priority. Understanding the priority of these rules is the key to configuring Nginx routing.
2.1 Exact match (=)
Exact match means that the requested URI must exactly match the location block in the configuration. If the URI is exactly the same as the string of the location block, this location block will be matched.
For example:
location = /hello { return 200 "Hello, World!";}
- When accessing /hello, Nginx will return Hello, World!, and no other URI (such as /hello/) will match the location block.
2.2 Prefix matching (/)
Prefix matching is the most common way to match in Nginx, which matches the prefix part of the URI.
For example:
location /images/ { root /var/www/images;}
- This location block is matched when the requested URI starts with /images/ (for example /images/). It should be noted that the prefix match isFrom left to rightMaking matching,And Nginx will look for the longest prefix match。
2.3 Regular matching (~ and * and ^)
Nginx also supports the use of regular expressions for matching. Regular expression matching is often used in more complex scenarios, such as matching dynamic paths or query parameters.
- ~ Indicates a case-sensitive regular match.
- ~* Indicates a case-insensitive regular match.
- ^~ Prefix matching tags(~)For example
location ~ .php$ { fastcgi_pass unix:/var/run/php/php7.;}
- This rule matches all requests ending in .php and forwards them to PHP-FPM for processing. Regular matches extract information from the requested URI based on regular expressions in the location block.
2.4 Regular Match Priority
Regular matches have a higher priority. At the same level, if the regular match is successful, Nginx will immediately use the location block and no longer continue to look up other rules downwards.
location ~* .jpg$ { root /var/www/images;} location /images/ { root /var/www/images;}
2.5 Longest prefix matching
When multiple location rules can match the same request, Nginx will select the longest prefix matching rule. If the prefix matches the same length, select the first rule.
location /images/ { root /var/www/images;} location /images/photo/ { root /var/www/photos;}
- For requests /images/photo/, Nginx selects the /images/photo/ rule because it is the longest prefix match.
2.6 Default
Nginx also provides a default location rule for handling requests that cannot match any specific rules. In the location block, you can use default to define the default behavior:
location / { root /var/www/html;} location /default { return 404;}
- If there are no requests that match any other rules, the default location block will be processed.
3. Priority of Nginx routing matching rules
It is very important to understand the priority of Nginx routing matching. Here is the priority order of Nginx matching rules:
- Exact match (=): Nginx first checks all location rules for exact matches. If a match is found, use the rule directly.
- Regular Match (~ and ~): Secondly, Nginx will look for all regular match location rules and apply regular expressions in order. Nginx will first match case-sensitive regular expression(), and then match case-insensitive regular expression().
- Prefix match (/): If the above rules do not match, Nginx will continue to look for the longest prefix match. The longest prefix match will be selected first.
- Default Match: If there is no matching rule, Nginx will use the default location block (usually location/) to handle the request.
The following shows the application of different matching rules:
server { listen 80; server_name ; # Exact match location = /hello { return 200 "Hello, World!"; } # Regular matching location ~* .jpg$ { root /var/www/images; } # Prefix matching location /images/ { root /var/www/images; } # Default matching location / { root /var/www/html; }}
- In this example, Nginx first checks if there is an exact match /hello path, and if it matches, it returns "Hello, World!". If the request path ends in .jpg, the regular expression rules are matched and the image is returned. If the first two rules are not matched, a /images/ path will be attempted, and if it is not matched, the default /path will be returned.
Other matching examples
location ~ ^/(cn|tw|de|fr|es|pt|jp|it|ar)/ai/object-remover/ { rewrite ^/(cn|tw|de|fr|es|pt|jp|it|ar)/ai/object-remover/(.*)$ /$1/application/object-remover/$2 break; proxy_pass ; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # Rewrite all requests to use as the base URL}
- This rule will match all requests for /cn/ai/object-remover/, /tw/ai/object-remover/, /fr/ai/object-remover/, etc.
The functions of the entire section configuration are:
- URI matching and rewrite:
- Any requests that match paths such as /cn/ai/object-remover/, /tw/ai/object-remover/, will be rewritten.
- The rewrite path will become /language code/application/object-remover/or the original path, such as /cn/application/object-remover/abc/xyz.
- Request forwarding:
- Rewrite requests will be forwarded to via proxy_pass.
- Request header settings:
- When forwarding the request, the original requested Host, client's X-Real-IP, and X-Forwarded-For headers are set.
4. Summary
Nginx's routing matching rules are requested through exact matches, prefix matches, regular matches and default matches. Understanding the priority and usage scenarios of these matching rules will help you configure Nginx more efficiently and avoid potential errors. By flexibly combining these matching rules, you can implement very complex routing logic to meet various business needs.
The above is the detailed explanation of Nginx routing matching rules and priority. For more information about Nginx routing matching rules, please pay attention to my other related articles!