【问题标题】:Is there a way to differentiate between location / {} and location /index {} in nginx?有没有办法区分 nginx 中的 location / {} 和 location /index {} ?
【发布时间】:2017-05-10 16:58:16
【问题描述】:

我被困在这两者之间:

location / { 
#proxy pass to some other location that handles all urls except /index
}
location /index {
    #logic to handle the index
 }

目前当我第一次运行 nginx 时

位置/ 块被执行,我被重定向到其他机器和

位置/索引 没有被调用。有没有办法区分这两者,或者我必须专门写所有位置。

【问题讨论】:

  • location /index 只会在你发出example.com/index 之类的请求时执行,否则/ location 将被执行

标签: nginx proxy http-proxy nginx-location


【解决方案1】:

是的,有区别。 location /{} 处理所有 url。但是当您放置 location /index {} 时,这意味着您的 location /{} 处理除 /index 之外的所有 url

现在,当您点击域 url(www.example.com) 时,您的这个位置 /{} 块处理 url,但是当您点击域/索引时,您的位置 /index {} 块处理 url。 除非您点击 domain/index(www.example.com/index)

,否则不会调用 location /index

希望你能得到答案

【讨论】:

  • 知道了! nginx 首先选择最长的匹配模式,如果路由没有得到解决,它会检查较短的模式
猜你喜欢
  • 2018-07-01
  • 2020-08-10
  • 2018-05-09
  • 1970-01-01
  • 1970-01-01
  • 2021-03-21
  • 2018-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多