Nginx:我如何转发一个HTTP请求到另一个端口?

我想要做的是,

当有人访问http://localhost/route/abc ,服务器响应与http://localhost:9000/abc完全相同

现在我configuration我的Nginx像这样:

 location /route { proxy_pass http://127.0.0.1:9000; } 

http请求正确调度端口9000 ,但是它接收的path是http://localhost:9000/route/abc而不是http://localhost:9000/abc

任何build议?

我相信你可以使用重写删除URL的额外部分。 在你的情况下,我认为你可以使用:

 location /route/ { rewrite ^/route/?(.*)$ /$1 break; proxy_pass http://127.0.0.1:9000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } 

但是,如果你的应用程序有内部链接,它们可能仍然指向/ abc / foo,如果你这样做,他们需要指向/ route / abc / foo,以便原始请求正确地进入。 如果你愿意的话,你最好离开nginx的configuration,而不是像configuration你的应用程序那样意识到它存在于一个子目录中。

我知道这是一个古老的问题,但当我试图解决同样的问题时,这是我的顶级谷歌打击!

我讨厌这里的微妙之处,但是试着像下面那样加9000。 现在不再将“路由”附加到转发的请求。

 location /route { proxy_pass http://127.0.0.1:9000/; } 

尝试以下

 location /route/ { proxy_pass http://127.0.0.1:9000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } 

vim nginx.conf

 http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 8080; location / { proxy_pass http://compute-1-36:8787; proxy_redirect http://compute-1-36:8787/ $scheme://$host:8080/; } } 

此代码在8080上侦听并redirect到compute-1-36上的端口8787。 您可以select其他path的位置/