如果我有一个域,例如http://www.example.com
,我想将所有来自http://www.example.com/test
请求redirect到http://www.example.com:3000
,我该如何正确执行?
我已经尝试了以下内容:
location /test { proxy_pass http://www.example.com:3000; proxy_set_header Host $host; }
但是,它所做的实际上是将http://www.example.com/test
redirect到http://www.example.com:3000/test
,这不是我想要的。
我怎样才能正确地做到这一点?
更新:
当Krizna
的答案奏效时,它将我redirect到我的域名。
但我现在想要的是我的浏览器栏是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; }
更新:如果您不想重写网址,请尝试下面的代码
server { -------- server_name www.example.com; location /test { proxy_pass http://example.com:3000; } }
proxy_redirect off
应该解决您的问题。 它会通过但不会改变URI。
文档: http : //wiki.nginx.org/HttpProxymodulee#proxy_redirect
location /test { proxy_pass http://example.com:3000; proxy_set_header Host $host; proxy_redirect off; }