nginx 根据请求参数动态proxy_pass后端域名
原创
©著作权归作者所有:来自51CTO博客作者小吉猫w的原创作品,请联系作者获取转载授权,否则将追究法律责任
方法一
server {
listen 80;
server_name __proxy_resolver__;
resolver 223.5.5.5;
/?proxy_schema=http&proxy_host=x.x.x.x:8080&proxy_url=/XXX/api?tId=123456&fooid=1234
set $proxy_schema 'http';
set $proxy_host '';
set $proxy_url '';
if ( $request_uri ~ (proxy_schema=([^&]+))){
set $proxy_schema $2;
}
if ( $request_uri ~ (proxy_host=([^&]+))){
set $proxy_host $2;
}
if ($request_uri ~ (proxy_url=(.*)$)){
set $proxy_url $2;
}
if ($proxy_url = '') {
return 404;
}
if ($proxy_host = '') {
return 404;
}
location / {
proxy_pass $proxy_schema://$proxy_host$proxy_url;
}
}
方法二
http://192.168.174.120/wbimg/?url=https://192.168.174.121/orj360/005UGUPIly1gwikvqim9rj31401404aa.jpg
server {
listen 80;
server_name 192.168.174.120;
resolver 223.5.5.5;
location /~wbimg {
if ($query_string ~* ^(.)url=(.)$) {
set $url_ $2; #$2=https://192.168.174.121/orj360/005UGUPIly1gwikvqim9rj31401404aa.jpg
}
proxy_set_header Referer "https://weibo.com";
proxy_pass $url_;
}
}
URL截取 跳转
location ~ ^/token/(.*)$ {
return 302 http://www.baidu.com/top?tokenId=$1;
}