简体   繁体   中英

Variable capture with parenthesis in Nginx location matching

I have this location rule:

location ~ ^/banks/(?P<region>[\w/~\-]+?)/responses/?$ {
    rewrite ^ $scheme://${host}/services/responses/list/city/${region}/? permanent;
}

and it works perfect for urls like :

http://example.com/banks/Arhangel~skaya_oblast~/Mirnyiy/responses/

But now I need to modify my location to match urls with parenthesis inside it. I use this version:

location ~ ^/banks/(?P<region>[\w/~\-()]+?)/responses/?$ {
    rewrite ^ $scheme://${host}/services/responses/list/city/${region}/? permanent;
}

But there are no rewrite happened for url /banks/Respublika_Saha_(Yakutiya)/Neryungri/responses/ .

I check this pattern with php preg_match function:

preg_match('|^/banks/(?P<region>[\w/~\-()]+?)/responses/?$|',
    '/banks/Respublika_Saha_(Yakutiya)/Neryungri/responses/');

And it returns 1 , so this pattern matches url with ( and ) .

Moreover I check this string with python re.search:

import re
matches = re.search('|^/banks/(?P<region>[\w/~\-()]+?)/responses/?$|','/banks/Respublika_Saha_(Yakutiya)/Neryungri/responses/')
print matches.start()

And it returns 0 , so this pattern matches this string from the beginning.

But nginx doesn't think so.

How should I modify nginx location rule to match this url /banks/Respublika_Saha_(Yakutiya)/Neryungri/responses/ ?

PS my version of nginx is nginx/1.9.10

Sorry for this question. Nginx works perfectly. And the problem wasn't with nginx but with our devops (almost no dev and very bad ops). Below I add scheme of our system. In few words, the system consist of these parts: nginx balancer, apache legacy system, nginx for new soa-based system, nginxes for services.

I consulted with devops and they told me to change certain nginx config in one of our git repos. I've changed it keeping in mind balancer config, but our devops forgot about nginx balancer, because it doesn't under version control. So I've changed nginx configs for new SOA system, but this redirect should send our users from old to new system on the balancer stage.

I was very upset by our devops, so Nginx works but their doesn't:)

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM