nginx之rewrite匹配需求

现在需求如下:

nginx上配有aaa.example.com的虚拟主机,现在需要将访问http://aaa.example.com/api/x.x/client/的请求转到http://bbb.example.com/api/x.x/client/,bbb.example.com的虚拟主机在另外一台nginx上,其中x.x表示位数不定的版本号,如:1.0或1.20.345都可能。请求转过去要求url保持不变

server {
listen 80;
server_name localhost;    localhost为192.168.223.136

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm index.php;
}
location ^~/api/ {
root html;
rewrite ^/api/([0-9]+.[0-9]+)/client/(.*)$ http://192.168.223.137/api/$1/client/$2 redirect;
}

location ^~/api/ {
root html;
index index.html;
rewrite ^/api/([0-9]+.[0-9]+)/client/(.*)$ http://192.168.223.137/api/$1/client/$2 redirect;
}
第一个小括号匹配的为$1
第二个小括号匹配的为$2
 

然后测试匹配结果:

此篇博文持续更新。。。。

http://www.linuxidc.com/Linux/2014-01/95493.htm

原文地址:https://www.cnblogs.com/jsonhc/p/7199436.html