Nginx - location configuration about Url

Basic knowledge

  1. Nginx location configuration syntax

        1. location [ = | ~ | ~* | ^~ ] uri { ... }
        2. location @name { ... }    
    1. There are two configuration methods for location configuration

      1.前缀 + uri(字符串/正则表达式)
      2.@ + name
      
    2. prefix meaning

          =  :精确匹配(必须全部相等)
          ~  :大小写敏感
          ~* :忽略大小写
          ^~ :只需匹配uri部分
          @  :内部服务跳转
  2. Location Basics

    1.location is configured in the server block.
    2. Different configurations (configured in location) can be used according to different URIs to process different requests.
    3. Locations are ordered and will be processed by the first matching location.

Location location demo

1. =, exact match

        location = / {
            #规则
        }
        # 则匹配到 `http://www.example.com/` 这种请求。 

2. ~, case sensitive

        location ~ /Example/ {
                #规则
        }
        #请求示例
        #http://www.example.com/Example/  [成功]
        #http://www.example.com/example/  [失败]

3. ~*, case ignored

    location ~* /Example/ {
                #规则
    }
    # 则会忽略 uri 部分的大小写
    #http://www.example.com/Example/  [成功]
    #http://www.example.com/example/  [成功]

4. ^~, only matches starting with uri

    location ^~ /img/ {
            #规则
    }
    #以 /img/ 开头的请求,都会匹配上
    #http://www.example.com/img/a.jpg   [成功]
    #http://www.example.com/img/b.mp4 [成功]

5. @, nginx internal jump

    location /img/ {
        error_page 404 @img_err;
    }
    
    location @img_err {
        # 规则
    }
    #以 /img/ 开头的请求,如果链接的状态为 404。则会匹配到 @img_err 这条规则上。

Summarize

The location in Nginx is not as difficult to understand as imagined, so don't be afraid. Look for more information, try more. You will gain.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324647581&siteId=291194637