Nginx configuration pitfalls: Be sure to pay attention to whether location and proxy_pass end with "/"

16961610:

Nginx is an open source, high-performance, highly reliable web and reverse proxy server, and supports hot deployment. It can run almost 24 hours a day, 7 hours a day. It does not need to be restarted even if it runs for several months, and it can still run without interruption. Hot update the software version under the condition of service. Performance is the most important consideration for Nginx. It takes up less memory, has strong concurrency capabilities, and can support up to 50,000 concurrent connections. Most importantly, Nginx is free and can be commercialized, and it is relatively simple to configure and use.

When nginx configures the proxy, some key points are often ignored. So what is the difference between the URL of the location matching request with "/" and without "/"? Let me popularize it here

1. Location configuration

1. Basic introduction

The location configuration is used to match the URL of the request, that is, the $request_uri variable in ngnix. Its configuration format is as follows:

location [space | = | ~ | ~* |^~|!~ | !~* ] /uri/ {}

 2. location matching order

(1) The location matching format rule prefixes are as follows:

  • =Starts with: indicates exact match
  • Beginning with ^~: Note that this is not a regular expression, its purpose is to match better than regular expressions; if the location is the best match, regular expression detection will no longer be performed.
  • ~Begins with: indicates case-sensitive regular matching;
  • Starting with ~*: indicates case-insensitive regular matching
  • !~ && !~*: Indicates case-sensitive non-matching regular expressions and case-insensitive non-matching regular expressions

(2) If location does not have a prefix, it is an ordinary string match, for example:

  • /uri/ ordinary string matching
  • / absolute path root directory match, if there is no other match, any request will be matched

(3) The matching search order priority is as follows (the priority decreases from top to bottom):

Notice:

  • When a match is successful, the matching will be stopped immediately and the request will be processed according to the current matching rules.
  • Prioritizing search does not mean prioritizing hits!
  • String matching is searched first, but only the longest match is recorded (if ^~ is the longest match, it will be hit directly and the search for regular matches will stop), and then the search for regular matches will continue. If there is a regular match, the regular match will be hit. If there is no regular match, the longest string match is hit.
  • First match =
  • Secondly match^~
  • Then perform regular matching according to the order of the configuration file.
  • Finally, it is handed over to / for universal matching

(location =) > (location full path) > (location ^~ path) > (location ~,~* regular sequence) > (location partial starting path) > (/)

3. Examples of matching patterns and sequences

  • The beginning of location = /uri: = indicates exact matching, and it will only take effect if it matches exactly.
  • location ^~ /uri: ^~ performs prefix matching on the URL path and precedes the regular expression.
  • location ~ :pattern~ indicates case-sensitive regular matching.
  • location ~*: The beginning of pattern~* indicates case-insensitive regular matching.
  • location /uri: Without any modifier, it also means prefix matching, but after the regular match, if there is no regular match, the longest rule will be hit.
  • location /: Universal matching, any request that does not match other locations will be matched, equivalent to default in switch.

4. Whether location ends with "/"

In ngnix, location performs fuzzy matching.

  • When there is no "/" at the end, location/abc/def can match /abc/defghi requests, or it can also match /abc/def/ghi, etc.
  • When it ends with "/", location/abc/def/ cannot match the /abc/defghi request, but can only match requests like /abc/def/anything

2. proxy_pass proxy rule (whether it ends with "/")

(1) When configuring proxy_pass, when / is added to the following url, which is equivalent to an absolute path, Nginx will not add the matching path part in the location to the proxy uri.

For example, in the following configuration, we access http://IP/proxy/test.html, and the final proxy URL is http://127.0.0.1/test.html

(2) If there is no / after configuring proxy_pass, Nginx will add the matching path part to the proxy uri.

For example, in the following configuration, we access http://IP/proxy/test.html, and the final proxy URL is http://127.0.0.1/proxy/test.htm

Guess you like

Origin blog.csdn.net/duansamve/article/details/132788598