nginx之location

发布时间 2023-09-13 09:50:20作者: hasome

location作用:

  • 基于一个指令设置URI

基本语法

Syntax:	location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default:	—
Context:	server, location
  • = 精确匹配,如果找到匹配=号的内容,立即停止搜索,并立即处理请求(优先级最高)
  • ~ 区分大小写
  • ~* 不区分大小写
  • ^~ 只匹配字符串,不匹配正则表达式
  • @ 指定一个命名的location,一般用于内部重定义请求,location @name
    匹配是有优先级的,不是按照nginx的配置文件进行。

官方示例

location = / {
    [ configuration A ]
}
location / {
    [ configuration B ]
}
location /documents/ {
    [ configuration C ]
}
location ^~ /images/ {
    [ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
    [ configuration E ]
}
  • / 匹配A。
  • /index.html 匹配B
  • /documents/document.html 匹配C
  • /images/1.gif 匹配D
  • /documents/1.jpg 匹配的是E。