如果我有这样的代理位置:
location /proxy{
proxy_pass http://127.0.0.1:1234;
}
它只会转发到localhost:1234
。现在我想要的是这样的:
/proxy/5544/abcd/1234 -> localhost:5544/abcd/1234 /proxy/5353/xyz/555 -> localhost:5353/xyz/555
如何实现这一点?
更新
所以我想要的是遵循以下格式的动态端口映射:
/proxy/{port}/{path}
以下是更多映射示例:
localhost/proxy/1234/abcd/xyz -> localhost:1234/abcd/xyz
localhost/proxy/9999/1234/5678 -> localhost:9999/1234/5678
localhost/proxy/8080/sub-path/another-path -> localhost:8080/sub-path/another-path
答案1
有了笔迹,就没法测试了。大概思路是这样的。
location ~ /proxy/(\d+)/(.*)$ {
set $flag $1;
set $url $2;
set $default_port 1234;
if ($flag ~ 5544) {
set $default_port 555;
}
rewrite ^.*$ /$url break;
proxy_pass http://127.0.0.1:$default_port;
}
# curl http://127.0.0.1:555
555
# curl http://127.0.0.1:1234
1234
# curl http://127.0.0.1:80/proxy/5353/index.html
1234
# curl http://127.0.0.1:80/proxy/5544/index.html
555