加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

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

发布时间:2020-12-13 21:30:12 所属栏目: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/ $s

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

有帮助,但很奇怪……

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读