【问题标题】:nginx proxy redirection with port from uri带有来自uri的端口的nginx代理重定向
【发布时间】: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 更改为实际端口,则它适用于指定端口。如何使用正则表达式指定重定向端口?

提前致谢!

【问题讨论】:

    标签: regex redirect nginx


    【解决方案1】:

    跟进@alleb57 的回答 - 在配置的这一点上似乎没有localhost 的定义。我在整个配置中转换为使用http://127.0.0.1(超过localhost),您不必定义解析器。

    【讨论】:

      【解决方案2】:

      添加

      resolver 127.0.0.1;
      

      有帮助,但是很奇怪……

      【讨论】:

      • 对我不起作用:[error] 7#7: send() failed (111: Connection refused) while resolving, resolver: 127.0.0.1:53
      • @FelikZ 在我的 Ubuntu 20.04 服务器上,我必须使用 resolver 127.0.0.53;。运行 cat /etc/resolv.conf 以检查您的域名服务器的 IP。
      猜你喜欢
      • 2020-09-27
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 2020-08-12
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多