Nginx-将www.example.com/test重定向到www.example.com:3000/

Nginx-将www.example.com/test重定向到www.example.com:3000/,nginx,routing,Nginx,Routing,例如,如果我有一个域,http://www.example.com,我想重定向http://www.example.com/test至http://www.example.com:3000,如何正确执行它? 我尝试了以下方法: location /test { proxy_pass http://www.example.com:3000; proxy_set_header Host $host; } 但它实际上是重定向http://www.example.com/test至ht

例如,如果我有一个域,
http://www.example.com
,我想重定向
http://www.example.com/test
http://www.example.com:3000
,如何正确执行它?
我尝试了以下方法:

location /test {
    proxy_pass http://www.example.com:3000;
    proxy_set_header Host $host;
}
但它实际上是重定向
http://www.example.com/test
http://www.example.com:3000/test
,这不是我想要的。
我怎样才能做好呢

更新:
虽然
Krizna
的答案有效,但它会按预期将我重定向到我的域。
但我现在想要的是我的浏览器栏成为
http://www.example.com/test
而不是
http://www.example.com:3000
。若我理解正确,我应该将nginx设置为捕获响应,并通过用户请求的url将其发送回。如何执行此操作?

请尝试此代码

location / {
   rewrite ^/test(/.*)$ http://example.com:3000$1 permanent;
   proxy_set_header Host $host;
}
更新: 如果不想重写URL,请尝试以下代码

server {
    --------
    server_name  www.example.com;
    location /test {
    proxy_pass http://example.com:3000;
    }
}

proxy\u重定向关闭
应该可以解决您的问题。它将通过,但不会更改URI

文件:


奇怪的是,我的服务器中有这样的配置,工作起来很有魅力。您是否对侦听
端口3000有不同的服务器配置?因为您正在重定向到同一个url,但具有不同的端口;这样它就会重定向到
http://example.com:3000/test
。但保持浏览器栏。保留浏览器栏但重定向到
http://example.com:3000
?好的。。我已经测试过,当前更新的代码运行良好。。只需删除代理设置头主机$Host;线
location /test {
    proxy_pass http://example.com:3000;
    proxy_set_header Host $host;
    proxy_redirect off;
}