Regex 使用来自uri的端口的nginx代理重定向
我正在尝试使用nginx进行重定向。其思想是将uri/id_1234/重定向到某些端口的localhost:1234。固定端口的重定向:Regex 使用来自uri的端口的nginx代理重定向,regex,redirect,nginx,Regex,Redirect,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/; } 它很好用。现在我尝试将12
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;
有帮助,但很奇怪…从@alleb57的答案开始跟进-似乎在配置的这一点上没有对
localhost
的定义。我转换为使用http://127.0.0.1
在整个配置过程中(通过localhost
),您不必定义解析程序。对我不起作用:[错误]7#7:send()解析时失败(111:连接被拒绝),解析程序:127.0.0.1:53
@FelikZ在我的Ubuntu 20.04服务器上,我必须使用解析器127.0.0.53代码>。运行cat/etc/resolv.conf
检查名称服务器的IP。