nginx:我如何做一个重写(重定向)相对?(即不要乱动端口号!)

时间:2021-08-01 07:43:32

I'm using nginx 1.9.11 currently. I have a situation where my application is served up from a dynamic port via a load balancer, but the port on the app itself is set. It looks like this

我现在用的是nginx 1。9.11。我的应用程序是通过负载均衡器从一个动态端口提供的,但应用程序本身的端口是设置的。

Port [4000-4999] on load balancer -> instances of nginx all on port 80

I need to redirect /myapp to /myapp/ without losing the port information. But I don't know the port at the application level, because the apps are dynamically created and destroyed!

我需要重定向/myapp到/myapp/而不丢失端口信息。但是我不知道应用程序级别的端口,因为应用程序是动态创建和销毁的!

Here's the syntax of my rewrite rule:

这是我重写规则的语法:

rewrite ^/myapp /myapp/

What I get is:

我得到的是:

http://example.com:4000/myapp  -> http://example.com/myapp   # no port 4000!

I can't hardcode the port...because there's a thousand possibilities, and they change all the time! I need to stay on the same port, but I don't know what port we came in on. How do I get nginx to leave the port alone?

我无法硬编码端口…因为有千千万万的可能性,而且它们一直在变化!我需要呆在同一个港口,但我不知道我们在哪个港口。如何让nginx离开港口?


What I've tried already...

我已经试过了……

In http, server, and location sections:

在http、服务器和位置部分:

port_in_redirect off;

In the server section:

在服务器部分:

proxy_set_header Host $host:$server_port;

Also in the server section as well:

同样在服务器部分:

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect off;

1 个解决方案

#1


1  

Classic. After two days of efforts I write up a nice Stack Overflow post, and immediately afterwards something occurs to me, I try it, and it works.

经典。经过两天的努力,我写了一个不错的栈溢出贴子,然后我立刻想到了一些事情,我尝试了它,它起作用了。

Here's how I solved the problem: I changed the rewrite rule to look like this:

我是这样解决这个问题的:我将重写规则修改为如下所示:

rewrite ^/myapp $http_host/myapp/

And it worked!

并且它成功了!

#1


1  

Classic. After two days of efforts I write up a nice Stack Overflow post, and immediately afterwards something occurs to me, I try it, and it works.

经典。经过两天的努力,我写了一个不错的栈溢出贴子,然后我立刻想到了一些事情,我尝试了它,它起作用了。

Here's how I solved the problem: I changed the rewrite rule to look like this:

我是这样解决这个问题的:我将重写规则修改为如下所示:

rewrite ^/myapp $http_host/myapp/

And it worked!

并且它成功了!