Nginx location匹配規則


1、語法

location [=|~|~*|^~|@] /uri/ {
  ...
} 

2、說明

從上面的語法出發,可以了解到 location 可以區分為三個部分,接下來一個一個的研究一下。

1) [=||*|^~|@]
  • = : 表示精確匹配后面的url
  • ~ : 表示正則匹配,但是區分大小寫
  • ~* : 正則匹配,不區分大小寫
  • ^~ : 表示普通字符匹配,如果該選項匹配,只匹配該選項,不匹配別的選項,一般用來匹配目錄
  • @ : "@" 定義一個命名的 location,使用在內部定向時,例如 error_page

上面定義了幾個不同的符號,表示不同的匹配規則,那么先后順序呢?

  1. = 前綴的指令嚴格匹配這個查詢。如果找到,停止搜索;
  2. 所有剩下的常規字符串,最長的匹配。如果這個匹配使用 ^~ 前綴,搜索停止;
  3. 正則表達式,在配置文件中定義的順序;
  4. 如果第 3 條規則產生匹配的話,結果被使用。否則,使用第 2 條規則的結果。
測試示例1:
location = /world {
    return 600;
}

location = /hello {
    return 600;
}

location ~ /hellowo {
    return 602;
}

location ^~ /hello {
    return 601;
}
- 請求 localhost/world 返回600
- 請求 localhost/world2 localhost/test/world 返回其他
- 請求 localhost/hello  返回600
- 請求 localhost/hello/123 返回601
- 請求 localhost/hellow 返回601
- 請求 localhost/hellowo 返回601
- 請求 localhost/test/hellowo  返回602
- 請求 localhost/test/hello 返回其他

因此可以知道:

  • = 是精確完整匹配,且優先級最高;
  • 正則匹配時,如果 ~ 和 ^~ 同時匹配規則,則 ^~ 優先;
  • ^~ 這個規則不會匹配請求 url 中后面的路徑,如上面的 /test/hello 沒有匹配上
  • ^~ 不支持正則,和 = 相比,范圍更廣,hellowo 是可以被 ^~ 匹配,但是 = 不會匹配;
  • ~ 路徑中只要包含就可以匹配,如上面的 /test/hellowo 返回了 602
測試示例2:
location ~ /hello {
  return 602;
}

location ~ /helloworld {
  return 601;
}
- 請求 localhost/world/helloworld 返回 602
- 請求 localhost/helloworld 返回 602

調整上面的順序

location ~ /helloworld {
  return 601;
}

location ~ /hello {
  return 602;
}
- 請求 localhost/helloworld 返回601
- 請求 localhost/world/helloworld 返回601
- 請求 localhost/helloWorld 返回602

所以同時正則匹配時

  • 放在前面的優先匹配
  • 注意如果不區分大小寫時,使用 ~*
  • 盡量將精確匹配的放在前面
測試示例3:
location ^~ /hello/ {
  return 601;
}

location /hello/world {
  return 602;
}

這種場景中,存在一個沒有符合的路由規則,那么實際的測試是怎樣呢?

- http://localhost/hello/wor 返回601
- http://localhost/hello/world 返回602
- http://localhost/hello/world23 返回602
- http://localhost/hello/world/123 返回602

從上面的示例可以看出

  • 沒有符合時,全匹配是優先 ^~ 的
2) [uri]

這里主要填的是需要匹配的 path 路徑,根據前面的符號,這里可以填寫精確到 path 路徑,也可以填正則表達式,下面則主要針對正則進行說明

. : 匹配除換行符以外的任意字符
? : 重復0次或1次
+ : 重復1次或更多次
* : 重復0次或更多次
\d :匹配數字
^ : 匹配字符串的開始
$ : 匹配字符串的介紹
{n} : 重復n次
{n,} : 重復n次或更多次
[c] : 匹配單個字符c
[a-z] : 匹配a-z小寫字母的任意一個
小括號()之間匹配的內容,可以在后面通過$1來引用,$2表示的是前面第二個()里的內容。正則里面容易讓人困惑的是\轉義特殊字符。

路由轉發

請求 path 匹配只是第一步,匹配完成之后,如何將請求轉發給其它的 web 服務呢?

1、反向代理

通常可見的一種使用姿勢就是使用 nginx 代理請求,轉發到內部的其它 web 服務上

主要通過 prixy_pass 來實現

location ^~ /webs {
    proxy_pass http://127.0.0.1:8080/webs;
}

上面規則的含義是,將所有以 webs 開頭的請求,轉發到 8080 端口的 web 服務上。

上面是直接寫死轉發到一個 ip 上,如果是多個機器提供服務,可以這樣配置

## 下面放在http的括號內,作為第一層
upstream test.online {
    server 47.106.81.116:8080 weight=1;
    server 47.106.81.117:8080 weight=1;
}

location ^~ /webs {
    proxy_pass http://test.online;
    proxy_redirect default;
}

2、Rewrite 命令

rewrite功能就是,使用nginx提供的全局變量或自己設置的變量,結合正則表達式和標志位實現url重寫以及重定向。

rewrite只能放在server{},location{},if{}中,並且只能對域名后邊的除去傳遞的參數外的字符串起作用, 如

http://blog.muzixizao.com/a/we/index.php?id=1&u=str

只對/a/we/index.php重寫。

語法: rewrite regex replacement [flag];

示例:

location ^~ /hexo {
  root '/Users/yihui/GitHub/';
}

location ~ /hello {
  rewrite ^(/hello).*$ /hexo/public/index.html last;
  return 603;
}

將hello開頭的,全部轉發到/hexo/public/index.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM