简体   繁体   English

nginx代理重定向与uri的端口

[英]nginx proxy redirection with port from uri

I'm trying to make redirections using nginx. 我正在尝试使用nginx进行重定向。 The idea is to redirect uri /id_1234/ to localhost:1234 for some ports. 想法是将uri / id_1234 /重定向到localhost:1234以用于某些端口。 The redirection for the fixed port: 固定端口的重定向:

location /id_1234/ {
    rewrite ^/id_1234/(.*) /$1 break;
    proxy_pass http://localhost:1234;
    proxy_redirect http://localhost:1234/ $scheme://$host/id_1234/;
}

It works just fine. 它工作得很好。 Now I try to change 1234 to any port: 现在我尝试将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/;
}

With this config, I get a 502 error, with the following error in log: 使用此配置,我收到502错误,日志中出现以下错误:

no resolver defined to resolve localhost

If I change $1 to actual port after localhost:, it works fine for the specified port. 如果我在localhost:之后将$ 1更改为实际端口,则它适用于指定的端口。 How can be redirection port specified using regex? 如何使用正则表达式指定重定向端口?

Thanks in advance! 提前致谢!

Adding 添加

resolver 127.0.0.1;

helps, but it's very strange... 有帮助,但很奇怪......

Following up from @alleb57's answer - it appears that there just isn't a definition for localhost at this point of the configuration. 跟随@ alleb57的回答 - 似乎在配置的这一点上没有localhost的定义。 I converted to using http://127.0.0.1 throughout the config (over localhost ) and you don't have to define the resolver. 我在整个配置中转换为使用http://127.0.0.1 (通过localhost ),您不必定义解析器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM