You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2020/11/05 00:56:30 UTC

[apisix] branch master updated: fix: after proxy-mirror plugin mirroring request, the host in the mir… (#2626)

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 b8e853c  fix: after proxy-mirror plugin mirroring request, the host in the mir… (#2626)
b8e853c is described below

commit b8e853cc392941bb9ff5f46f3f703f340382c56e
Author: Firstsawyou <52...@users.noreply.github.com>
AuthorDate: Thu Nov 5 08:56:18 2020 +0800

    fix: after proxy-mirror plugin mirroring request, the host in the mir… (#2626)
    
    fix #2617
---
 apisix/cli/ngx_tpl.lua          |  1 +
 apisix/plugins/proxy-mirror.lua |  1 +
 t/APISIX.pm                     |  1 +
 t/plugin/proxy-mirror.t         | 74 +++++++++++++++++++++++++++++++++++++++--
 4 files changed, 75 insertions(+), 2 deletions(-)

diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua
index dc9d9cc..acc0f5f 100644
--- a/apisix/cli/ngx_tpl.lua
+++ b/apisix/cli/ngx_tpl.lua
@@ -498,6 +498,7 @@ http {
                 return 200;
             }
 
+            proxy_set_header Host $upstream_host;
             proxy_pass $upstream_mirror_host$request_uri;
         }
         {% end %}
diff --git a/apisix/plugins/proxy-mirror.lua b/apisix/plugins/proxy-mirror.lua
index 830f889..760c561 100644
--- a/apisix/plugins/proxy-mirror.lua
+++ b/apisix/plugins/proxy-mirror.lua
@@ -52,6 +52,7 @@ end
 function _M.rewrite(conf, ctx)
     core.log.info("proxy mirror plugin rewrite phase, conf: ", core.json.delay_encode(conf))
 
+    ctx.var.upstream_host = ctx.var.host
     ctx.var.upstream_mirror_host = conf.host
 end
 
diff --git a/t/APISIX.pm b/t/APISIX.pm
index 86e55ee..13c7ffe 100644
--- a/t/APISIX.pm
+++ b/t/APISIX.pm
@@ -436,6 +436,7 @@ _EOC_
                 return 200;
             }
 
+            proxy_set_header Host \$upstream_host;
             proxy_pass \$upstream_mirror_host\$request_uri;
         }
 _EOC_
diff --git a/t/plugin/proxy-mirror.t b/t/plugin/proxy-mirror.t
index 1425fc9..a9c438d 100644
--- a/t/plugin/proxy-mirror.t
+++ b/t/plugin/proxy-mirror.t
@@ -32,8 +32,15 @@ add_block_preprocessor(sub {
         server_tokens off;
 
         location / {
-            content_by_lua_block {
-                ngx.log(ngx.ERR, "uri: ", ngx.var.uri)
+            content_by_lua_block {            
+                local core = require("apisix.core")
+
+                local headers_tab = ngx.req.get_headers()               
+                for k, v in pairs(headers_tab) do
+                    core.log.info(k, ": ", v)
+                end
+
+                core.log.info("uri: ", ngx.var.uri)
                 ngx.say("hello world")
             }
         }
@@ -289,3 +296,66 @@ GET /hello
 hello world
 --- error_log
 uri: /hello
+
+
+
+=== TEST 8: sanity check (normal case), and uri is "/uri"
+--- config
+       location /t {
+           content_by_lua_block {
+               local t = require("lib.test_admin").test
+               local code, body = t('/apisix/admin/routes/1',
+                    ngx.HTTP_PUT,
+                    [[{
+                        "plugins": {
+                            "proxy-mirror": {
+                               "host": "http://127.0.0.1:1986"
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/uri"
+                   }]]
+                   )
+
+               if code >= 300 then
+                   ngx.status = code
+               end
+               ngx.say(body)
+           }
+        }
+--- request
+GET /t
+--- error_code: 200
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 9: the request header does not change
+--- request
+GET /uri
+--- error_code: 200
+--- more_headers
+host: 127.0.0.2
+api-key: hello
+api-key2: world
+name: jake
+--- response_body
+uri: /uri
+host: 127.0.0.2
+x-real-ip: 127.0.0.1
+name: jake
+api-key: hello
+api-key2: world
+--- error_log
+host: 127.0.0.2
+api-key: hello
+name: jake
+api-key2: world