Priority of nginx location configuration

      

location regex

location  = / {
  # 精确匹配 / ,主机名后面不能带任何字符串
  [ configuration A ]
}
location  / {
  # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求
  # 但是正则和最长字符串会优先匹配
  [ configuration B ]
}
location /documents/ {
  # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
  # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
  [ configuration C ]
}
location ~ /documents/Abc {
  # 匹配任何以 /documents/Abc 开头的地址,匹配符合以后,还要继续往下搜索
  # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
  [ configuration CC ]
}
location ^~ /images/ {
  # 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。
  [ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
  # 匹配所有以 gif,jpg或jpeg 结尾的请求
  # 然而,所有请求 /images/ 下的图片会被 config D 处理,因为 ^~ 到达不了这一条正则
  [ configuration E ]
}
location /images/ {
  # 字符匹配到 /images/,继续往下,会发现 ^~ 存在
  [ configuration F ]
}
location /images/abc {
  # 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在
  # F与G的放置顺序是没有关系的
  [ configuration G ]
}
location ~ /images/abc/ {
  # 只有去掉 config D 才有效:先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用
    [ configuration H ]
}
location ~* /js/.*/\.js
  • starts with an exact match
  • For example, in A, only requests at the end of the root directory can be matched, and no strings can be followed.
  • The beginning of ^~ means that the uri starts with a regular string, not a regular match
  • ~ starts with a case-sensitive regular match;
  • ~* at the beginning means case-insensitive regular matching
  • / Universal match, if no other match, any request will match

order priority

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

The above matching results, according to the above location writing, the following matching examples are established
/ -> config A is an exact match, even if /index.html can not match
/downloads/download.html ->
After config B matches B, there is no further down. Any match, use B
/images/1.gif -> configuration D
to match to F, down to D, stop down
/images/abc/def -> config D
to match the longest to G, down to match D, stop Going down
you can see that anything starting with /images/ will match D and stop, FG is meaningless here, H will never turn, here is just to illustrate the matching order
/documents/document.html -> config C
matches C, there is no match down, use C
/documents/1.jpg -> configuration E
matches C, down to E
/documents/Abc.jpg -> config CC
matches the longest C, the regular sequence matches to CC down, not down to E

Practical usage advice

所以实际使用中,个人觉得至少有三个匹配规则定义,如下:
#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
#这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
location = / {
    proxy_pass http://tomcat:8080/index
}
# 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项
# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
    root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
    root /webroot/res/;
}
#第三个规则就是通用规则,用来转发动态请求到后端应用服务器
#非静态文件请求就默认是动态请求,自己根据实际把握
#毕竟目前的一些框架的流行,带.php,.jsp后缀的情况很少了
location / {
    proxy_pass http://tomcat:8080/
}

Attach two addresses:
http://tengine.taobao.org/book/chapter_02.html
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

Guess you like

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