我這個位置的規則括號:變量捕獲與Nginx的位置相匹配,
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
,所以這個模式匹配的網址與(
和)
。
而且我檢查這個字符串使用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/
?
P.S.我的nginx版本是nginx/1.9.10
但它的工作原理:[https://regex101.com/r/vI6jZ4/1]( https://regex101.com/r/vI6jZ4/1) –
是的,這個模式對於php'preg_match'或python're.search'可以正常工作,但是不適用於nginx位置規則 –
適用於'nginx' 1.8。 0 –