Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
nginx:如何使重写(重定向)相对?(即不要弄乱端口号!)_Nginx_Rewrite - Fatal编程技术网

nginx:如何使重写(重定向)相对?(即不要弄乱端口号!)

nginx:如何使重写(重定向)相对?(即不要弄乱端口号!),nginx,rewrite,Nginx,Rewrite,我目前正在使用nginx1.9.11。我的应用程序通过负载平衡器从动态端口提供服务,但应用程序本身的端口已设置。看起来像这样 Port [4000-4999] on load balancer -> instances of nginx all on port 80 我需要将/myapp重定向到/myapp/,而不会丢失端口信息。但我不知道应用程序级别的端口,因为应用程序是动态创建和销毁的 以下是我的重写规则的语法: rewrite ^/myapp /myapp/ 我得到的是: htt

我目前正在使用nginx1.9.11。我的应用程序通过负载平衡器从动态端口提供服务,但应用程序本身的端口已设置。看起来像这样

Port [4000-4999] on load balancer -> instances of nginx all on port 80
我需要将
/myapp
重定向到
/myapp/
,而不会丢失端口信息。但我不知道应用程序级别的端口,因为应用程序是动态创建和销毁的

以下是我的重写规则的语法:

rewrite ^/myapp /myapp/
我得到的是:

http://example.com:4000/myapp  -> http://example.com/myapp   # no port 4000!
我不能硬编码端口…因为有一千种可能性,而且它们一直在变化!我需要呆在同一个港口,但我不知道我们在哪个港口。如何让nginx离开端口


我已经尝试过的…

在http、
服务器
位置
部分:

port_in_redirect off;
服务器
部分:

proxy_set_header Host $host:$server_port;
同样在
服务器
部分:

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;

经典。经过两天的努力,我写了一篇很好的堆栈溢出帖子,之后我马上想到了一些事情,我尝试了一下,它成功了

下面是我解决问题的方法:我将重写规则更改为如下所示:

rewrite ^/myapp $http_host/myapp/
成功了