nginx proxy redirection with port from uri

匿名 (未验证) 提交于 2019-12-03 00:53:01

问题:

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/; } 

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/; } 

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

no resolver defined to resolve 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?

Thanks in advance!

回答1:

Adding

resolver 127.0.0.1; 

helps, but it's very strange...



回答2:

Following up from @alleb57's answer - it appears that there just isn't a definition for localhost at this point of the configuration. I converted to using http://127.0.0.1 throughout the config (over localhost) and you don't have to define the resolver.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!