从uri端口进行nginx代理重定向 [英] nginx proxy redirection with port from uri

查看:413
本文介绍了从uri端口进行nginx代理重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用nginx进行重定向.这个想法是将uri/id_1234/重定向到某些端口的localhost:1234.固定端口的重定向:

I'm trying to make redirections using nginx. The idea is to redirect uri /id_1234/ to localhost:1234 for some ports. 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/;
}

效果很好.现在,我尝试将1234更改为任何端口:

It works just fine. Now I try to change 1234 to any port:

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错误,并在日志中显示以下错误:

With this config, I get a 502 error, with the following error in log:

no resolver defined to resolve localhost

如果我将$ 1更改为localhost:之后的实际端口,则在指定端口上可以正常工作.如何使用正则表达式指定重定向端口?

If I change $1 to actual port after localhost:, it works fine for the specified port. How can be redirection port specified using regex?

提前谢谢!

推荐答案

添加

resolver 127.0.0.1;

有帮助,但这很奇怪...

helps, but it's very strange...

这篇关于从uri端口进行nginx代理重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆