nginx之rewrite匹配需求

wadeson發表於2017-07-18

現在需求如下:

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

相關文章