Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用来自uri的端口的nginx代理重定向_Regex_Redirect_Nginx - Fatal编程技术网

Regex 使用来自uri的端口的nginx代理重定向

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

我正在尝试使用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;

有帮助,但很奇怪…

从@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。