正则表达式 – 与uri端口的nginx代理重定向

我正在尝试使用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更改为实际端口,则它适用于指定的端口.如何使用正则表达式指定重定向端口?

提前致谢!

最佳答案
添加

resolver 127.0.0.1;

有帮助,但很奇怪……

dawei

【声明】:淮南站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。