nginx的匹配类似,但是位置错误

我有几个暴露的JSON脚本/程序从服务器快速收集信息。 我使用nginx将这些只是原始的URL。 所以给以下位置:

 location ~ ^/api/status/? { rewrite ^(.*)$ /path/to/some/handler/wan.handler; } location ~ ^/api/status-lan/? { rewrite ^(.*)$ /path/to/some/handler/lan.handler; } 

为什么nginxstatus-lan调用与status位置匹配? 所以我可以从/api/status/查看我的输出,但是如果我查看/api/status-lan/我得到status位置。

为什么nginx将status-lan调用与状态位置匹配?

因为该位置是一个正则expression式,所以这个正则expression式:

 location ~ ^/api/status/? { 

意思是“以/ api / status开头,后面跟着一个可选的结尾斜线”,匹配之后会发生什么并不重要。

configuration应该可能是:

 location ~ ^/api/status/?$ { rewrite ^ /path/to/some/handler/wan.handler; } location ~ ^/api/status-lan/?$ { rewrite ^ /path/to/some/handler/lan.handler; } 

即匹配整个url,而不仅仅是开始。 如果有疑问,打开重写日志,因为它会明确发生了什么,以及为什么。