【发布时间】:2016-07-12 03:30:15
【问题描述】:
我有这个位置规则:
location ~ ^/banks/(?P<region>[\w/~\-]+?)/responses/?$ {
rewrite ^ $scheme://${host}/services/responses/list/city/${region}/? permanent;
}
它非常适合以下网址:
http://example.com/banks/Arhangel~skaya_oblast~/Mirnyiy/responses/
但现在我需要修改我的位置以匹配带有括号的网址。我用这个版本:
location ~ ^/banks/(?P<region>[\w/~\-()]+?)/responses/?$ {
rewrite ^ $scheme://${host}/services/responses/list/city/${region}/? permanent;
}
但 url /banks/Respublika_Saha_(Yakutiya)/Neryungri/responses/ 没有发生重写。
我用 php preg_match 函数检查这个模式:
preg_match('|^/banks/(?P<region>[\w/~\-()]+?)/responses/?$|',
'/banks/Respublika_Saha_(Yakutiya)/Neryungri/responses/');
它返回1,所以这个模式匹配url与(和)。
此外,我用 python re.search 检查了这个字符串:
import re
matches = re.search('|^/banks/(?P<region>[\w/~\-()]+?)/responses/?$|','/banks/Respublika_Saha_(Yakutiya)/Neryungri/responses/')
print matches.start()
它返回0,所以这个模式从一开始就匹配这个字符串。
但是 nginx 不这么认为。
我应该如何修改 nginx 位置规则以匹配这个 url /banks/Respublika_Saha_(Yakutiya)/Neryungri/responses/ ?
附:我的 nginx 版本是nginx/1.9.10
【问题讨论】:
-
是的,这种模式适用于 php
preg_match或 pythonre.search,但不适用于 nginx 位置规则 -
在
nginx1.8.0 上为我工作 -
谢谢你们的帮助,我找到了答案。问题不在于 Nginx,而在于我们的 devops。我发布了答案,并详细描述了我们系统的情况和架构。
标签: php regex nginx pattern-matching nginx-location