This is an automated email from the ASF dual-hosted git repository.
spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new 84c95c80e feat: allow customize x_Forwarded_Port while handling
upstream headers (#8226)
84c95c80e is described below
commit 84c95c80eab72d093971a5a1376dda8c14c2faf7
Author: biakecw <[email protected]>
AuthorDate: Wed Nov 9 09:15:11 2022 +0800
feat: allow customize x_Forwarded_Port while handling upstream headers
(#8226)
Co-authored-by: caowang <[email protected]>
Fixes https://github.com/apache/apisix/issues/4942
---
apisix/cli/ngx_tpl.lua | 3 ---
apisix/init.lua | 5 +++++
t/APISIX.pm | 3 ---
t/plugin/proxy-rewrite2.t | 27 +++++++++++++++++++++++++++
4 files changed, 32 insertions(+), 6 deletions(-)
diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua
index cc849884e..d63f713db 100644
--- a/apisix/cli/ngx_tpl.lua
+++ b/apisix/cli/ngx_tpl.lua
@@ -677,9 +677,6 @@ http {
if ($http_x_forwarded_for != "") {
set $var_x_forwarded_for "${http_x_forwarded_for},
${realip_remote_addr}";
}
- if ($http_x_forwarded_port != "") {
- set $var_x_forwarded_port $http_x_forwarded_port;
- }
proxy_set_header X-Forwarded-For $var_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $var_x_forwarded_proto;
diff --git a/apisix/init.lua b/apisix/init.lua
index 9c1dde08c..6b51ab410 100644
--- a/apisix/init.lua
+++ b/apisix/init.lua
@@ -263,6 +263,11 @@ local function set_upstream_headers(api_ctx, picked_server)
if x_forwarded_host then
api_ctx.var.var_x_forwarded_host = x_forwarded_host
end
+
+ local port = api_ctx.var.http_x_forwarded_port
+ if port then
+ api_ctx.var.var_x_forwarded_port = port
+ end
end
diff --git a/t/APISIX.pm b/t/APISIX.pm
index 8c6924cf0..15de6af5c 100644
--- a/t/APISIX.pm
+++ b/t/APISIX.pm
@@ -771,9 +771,6 @@ _EOC_
if (\$http_x_forwarded_for != "") {
set \$var_x_forwarded_for "\${http_x_forwarded_for},
\${realip_remote_addr}";
}
- if (\$http_x_forwarded_port != "") {
- set \$var_x_forwarded_port \$http_x_forwarded_port;
- }
proxy_set_header X-Forwarded-For \$var_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$var_x_forwarded_proto;
diff --git a/t/plugin/proxy-rewrite2.t b/t/plugin/proxy-rewrite2.t
index bd95360f8..d2461138f 100644
--- a/t/plugin/proxy-rewrite2.t
+++ b/t/plugin/proxy-rewrite2.t
@@ -207,3 +207,30 @@ X-Forwarded-Proto: http
X-Forwarded-Proto: grpc
--- response_headers
X-Forwarded-Proto: http
+
+
+
+=== TEST 7: customize X-Forwarded-Port
+--- apisix_yaml
+routes:
+ -
+ id: 1
+ uri: /echo
+ plugins:
+ proxy-rewrite:
+ headers:
+ X-Forwarded-Port: 10080
+ upstream_id: 1
+upstreams:
+ -
+ id: 1
+ nodes:
+ "127.0.0.1:1980": 1
+ type: roundrobin
+#END
+--- request
+GET /echo
+--- more_headers
+X-Forwarded-Port: 8080
+--- response_headers
+X-Forwarded-Port: 10080