欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

nginx路径匹配

程序员文章站 2022-03-19 18:31:12
...

语法: location [=|~|~*|^~] /uri/ { ... }

 

  1. ~* 正则匹配不区分大小写
  2. ~ 正则匹配区分大小写
  3. ^~ 路径匹配,匹配后立即终止,不再尝试匹配正则
  4. = 完全匹配,匹配后立即终止,不再尝试其他任何匹配

优先级:

完全匹配 > 带^~的路径匹配 > 正则(多个正则时优先级按配置顺序) > 路径匹配

 

官方举例:

 

location  = / {

  # matches the query / only.
  [ configuration A ] 
}
location  / {
  # matches any query, since all queries begin with /, but regular
  # expressions and any longer conventional blocks will be

  # matched first.
  [ configuration B ] 
}
location ^~ /images/ {
  # matches any query beginning with /images/ and halts searching,
  # so regular expressions will not be checked.

  [ configuration C ] 
}
location ~* \.(gif|jpg|jpeg)$ {
  # matches any request ending in gif, jpg, or jpeg. However, all
  # requests to the /images/ directory will be handled by

  # Configuration C.   
  [ configuration D ] 
}

 

Example requests:

  • / -> configuration A
  • /documents/document.html -> configuration B
  • /images/1.gif -> configuration C
  • /documents/1.jpg -> configuration D