【发布时间】:2014-09-26 04:52:50
【问题描述】:
我正在尝试使用 nginx 进行重定向。这个想法是将 uri /id_1234/ 重定向到某些端口的 localhost:1234 。固定端口的重定向:
location /id_1234/ {
rewrite ^/id_1234/(.*) /$1 break;
proxy_pass http://localhost:1234;
proxy_redirect http://localhost:1234/ $scheme://$host/id_1234/;
}
它工作得很好。现在我尝试将 1234 更改为任何端口:
location ~* ^/id_([0-9]*)/ {
rewrite ^/id_([0-9]*)/(.*)$ /$2 break;
proxy_pass http://localhost:$1;
proxy_redirect http://localhost:$1/ $scheme://$host/id_$1/;
}
使用此配置,我收到 502 错误,日志中出现以下错误:
no resolver defined to resolve localhost
如果我在 localhost: 之后将 $1 更改为实际端口,则它适用于指定端口。如何使用正则表达式指定重定向端口?
提前致谢!
【问题讨论】: