我想要的效果:
http://hostname/proxy/3000
http://127.0.0.1:3000
http://hostname/proxy/3000/anything
http://127.0.0.1:3000/anything
有一個(gè)要求:端口是變化的
我嘗試了
location ~ /proxy/(\d+) {
proxy_pass http://127.0.0.1:;
rewrite ^/(.*)$ / break;
}
但是rewrite不管怎么寫(xiě)都有問(wèn)題
在nginx的配置里該怎么寫(xiě),在線(xiàn)等~
proxy_pass
的文檔里有講:location
使用了正則后,proxy_pass
指令后面的參數(shù)中的 URI 部分會(huì)被忽略??梢允褂孟旅娴呐渲瞄g接達(dá)到你想要的功能:
server {
listen 80;
server_name localhost;
location /proxy/ {
rewrite ^/proxy/(\d+)/(.*) /internal?port=&url= last;
}
location /internal {
internal;
proxy_pass http://127.0.0.1:$arg_port/$arg_url;
}
}