Skip to content

Commit db2077a

Browse files
authored
fix(proxy-mirror): use with uri rewrite (#8718)
Fixes #8167
1 parent 822d545 commit db2077a

File tree

2 files changed

+140
-6
lines changed

2 files changed

+140
-6
lines changed

apisix/plugins/proxy-mirror.lua

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,20 @@ end
8989

9090

9191
local function enable_mirror(ctx, conf)
92-
if conf.path and conf.path_concat_mode == "prefix" then
93-
ctx.var.upstream_mirror_uri = resolver_host(conf.host) .. conf.path .. ctx.var.uri ..
94-
ctx.var.is_args .. (ctx.var.args or '')
95-
else
96-
ctx.var.upstream_mirror_uri = resolver_host(conf.host) .. (conf.path or ctx.var.uri) ..
97-
ctx.var.is_args .. (ctx.var.args or '')
92+
local uri = (ctx.var.upstream_uri and ctx.var.upstream_uri ~= "") and
93+
ctx.var.upstream_uri or
94+
ctx.var.uri .. ctx.var.is_args .. (ctx.var.args or '')
95+
96+
if conf.path then
97+
if conf.path_concat_mode == "prefix" then
98+
uri = conf.path .. uri
99+
else
100+
uri = conf.path .. ctx.var.is_args .. (ctx.var.args or '')
101+
end
98102
end
99103

104+
ctx.var.upstream_mirror_uri = resolver_host(conf.host) .. uri
105+
100106
if has_mod then
101107
apisix_ngx_client.enable_mirror()
102108
end

t/plugin/proxy-mirror2.t

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
use t::APISIX 'no_plan';
18+
19+
repeat_each(1);
20+
no_long_string();
21+
no_shuffle();
22+
no_root_location();
23+
log_level('info');
24+
worker_connections(1024);
25+
26+
add_block_preprocessor(sub {
27+
my ($block) = @_;
28+
29+
my $http_config = $block->http_config // <<_EOC_;
30+
31+
server {
32+
listen 1986;
33+
server_tokens off;
34+
35+
location / {
36+
content_by_lua_block {
37+
local core = require("apisix.core")
38+
core.log.info("upstream_http_version: ", ngx.req.http_version())
39+
40+
local headers_tab = ngx.req.get_headers()
41+
local headers_key = {}
42+
for k in pairs(headers_tab) do
43+
core.table.insert(headers_key, k)
44+
end
45+
core.table.sort(headers_key)
46+
47+
for _, v in pairs(headers_key) do
48+
core.log.info(v, ": ", headers_tab[v])
49+
end
50+
51+
core.log.info("uri: ", ngx.var.request_uri)
52+
ngx.say("hello world")
53+
}
54+
}
55+
}
56+
_EOC_
57+
58+
$block->set_value("http_config", $http_config);
59+
60+
if (!$block->request) {
61+
$block->set_value("request", "GET /t");
62+
}
63+
});
64+
65+
run_tests;
66+
67+
__DATA__
68+
69+
=== TEST 1: use proxy-rewrite to change uri before mirror
70+
--- config
71+
location /t {
72+
content_by_lua_block {
73+
local t = require("lib.test_admin").test
74+
local code, body = t('/apisix/admin/routes/1',
75+
ngx.HTTP_PUT,
76+
[[{
77+
"plugins": {
78+
"proxy-rewrite":{
79+
"_meta": {
80+
"priority": 1010
81+
},
82+
"uri": "/hello"
83+
},
84+
"proxy-mirror": {
85+
"_meta": {
86+
"priority": 1008
87+
},
88+
"host": "http://127.0.0.1:1986"
89+
}
90+
},
91+
"upstream": {
92+
"nodes": {
93+
"127.0.0.1:1980": 1
94+
},
95+
"type": "roundrobin"
96+
},
97+
"uri": "/nope"
98+
}]]
99+
)
100+
101+
if code >= 300 then
102+
ngx.status = code
103+
end
104+
ngx.say(body)
105+
}
106+
}
107+
--- response_body
108+
passed
109+
110+
111+
112+
=== TEST 2: hit route (with proxy-rewrite)
113+
--- request
114+
GET /nope
115+
--- response_body
116+
hello world
117+
--- error_log
118+
uri: /hello
119+
120+
121+
122+
=== TEST 3: hit route (with proxy-rewrite and args)
123+
--- request
124+
GET /nope?a=b&b=c&c=d
125+
--- response_body
126+
hello world
127+
--- error_log
128+
uri: /hello?a=b&b=c&c=d

0 commit comments

Comments
 (0)