Skip to content

Commit 0befa29

Browse files
committed
fix rewrite path for prefix match
1 parent 938b7ff commit 0befa29

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

internal/mode/static/nginx/config/servers.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -620,22 +620,24 @@ func createRewritesValForRewriteFilter(filter *dataplane.HTTPURLRewriteFilter, p
620620
filterPrefix = "/"
621621
}
622622

623-
// capture everything after the configured prefix
624-
regex := fmt.Sprintf("^%s(.*)$", path)
625-
// replace the configured prefix with the filter prefix and append what was captured
626-
replacement := fmt.Sprintf("%s$1", filterPrefix)
623+
// capture everything following the configured prefix up to the first ?, if present.
624+
regex := fmt.Sprintf("^%s([^?]*)?", path)
625+
// replace the configured prefix with the filter prefix, append the captured segment,
626+
// and include the request arguments stored in nginx variable $args.
627+
// https://nginx.org/en/docs/http/ngx_http_core_module.html#var_args
628+
replacement := fmt.Sprintf("%s$1?$args?", filterPrefix)
627629

628630
// if configured prefix does not end in /, but replacement prefix does end in /,
629631
// then make sure that we *require* but *don't capture* a trailing slash in the request,
630632
// otherwise we'll get duplicate slashes in the full replacement
631633
if strings.HasSuffix(filterPrefix, "/") && !strings.HasSuffix(path, "/") {
632-
regex = fmt.Sprintf("^%s(?:/(.*))?$", path)
634+
regex = fmt.Sprintf("^%s(?:/([^?]*))?", path)
633635
}
634636

635637
// if configured prefix ends in / we won't capture it for a request (since it's not in the regex),
636638
// so append it to the replacement prefix if the replacement prefix doesn't already end in /
637639
if strings.HasSuffix(path, "/") && !strings.HasSuffix(filterPrefix, "/") {
638-
replacement = fmt.Sprintf("%s/$1", filterPrefix)
640+
replacement = fmt.Sprintf("%s/$1?$args?", filterPrefix)
639641
}
640642

641643
rewrites.MainRewrite = fmt.Sprintf("%s %s break", regex, replacement)

internal/mode/static/nginx/config/servers_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ func TestCreateServers(t *testing.T) {
12781278
},
12791279
{
12801280
Path: "/_ngf-internal-rule8-route0",
1281-
Rewrites: []string{"^ $request_uri", "^/rewrite-with-headers(.*)$ /prefix-replacement$1 break"},
1281+
Rewrites: []string{"^ $request_uri", "^/rewrite-with-headers([^?]*)? /prefix-replacement$1?$args? break"},
12821282
ProxyPass: "http://test_foo_80",
12831283
ProxySetHeaders: rewriteProxySetHeaders,
12841284
Type: http.InternalLocationType,
@@ -2441,7 +2441,7 @@ func TestCreateRewritesValForRewriteFilter(t *testing.T) {
24412441
},
24422442
expected: &rewriteConfig{
24432443
InternalRewrite: "^ $request_uri",
2444-
MainRewrite: "^/original(.*)$ /prefix-path$1 break",
2444+
MainRewrite: "^/original([^?]*)? /prefix-path$1?$args? break",
24452445
},
24462446
msg: "prefix path no trailing slashes",
24472447
},
@@ -2455,7 +2455,7 @@ func TestCreateRewritesValForRewriteFilter(t *testing.T) {
24552455
},
24562456
expected: &rewriteConfig{
24572457
InternalRewrite: "^ $request_uri",
2458-
MainRewrite: "^/original(?:/(.*))?$ /$1 break",
2458+
MainRewrite: "^/original(?:/([^?]*))? /$1?$args? break",
24592459
},
24602460
msg: "prefix path empty string",
24612461
},
@@ -2469,7 +2469,7 @@ func TestCreateRewritesValForRewriteFilter(t *testing.T) {
24692469
},
24702470
expected: &rewriteConfig{
24712471
InternalRewrite: "^ $request_uri",
2472-
MainRewrite: "^/original(?:/(.*))?$ /$1 break",
2472+
MainRewrite: "^/original(?:/([^?]*))? /$1?$args? break",
24732473
},
24742474
msg: "prefix path /",
24752475
},
@@ -2483,7 +2483,7 @@ func TestCreateRewritesValForRewriteFilter(t *testing.T) {
24832483
},
24842484
expected: &rewriteConfig{
24852485
InternalRewrite: "^ $request_uri",
2486-
MainRewrite: "^/original(?:/(.*))?$ /trailing/$1 break",
2486+
MainRewrite: "^/original(?:/([^?]*))? /trailing/$1?$args? break",
24872487
},
24882488
msg: "prefix path replacement with trailing /",
24892489
},
@@ -2497,7 +2497,7 @@ func TestCreateRewritesValForRewriteFilter(t *testing.T) {
24972497
},
24982498
expected: &rewriteConfig{
24992499
InternalRewrite: "^ $request_uri",
2500-
MainRewrite: "^/original/(.*)$ /prefix-path/$1 break",
2500+
MainRewrite: "^/original/([^?]*)? /prefix-path/$1?$args? break",
25012501
},
25022502
msg: "prefix path original with trailing /",
25032503
},
@@ -2511,7 +2511,7 @@ func TestCreateRewritesValForRewriteFilter(t *testing.T) {
25112511
},
25122512
expected: &rewriteConfig{
25132513
InternalRewrite: "^ $request_uri",
2514-
MainRewrite: "^/original/(.*)$ /trailing/$1 break",
2514+
MainRewrite: "^/original/([^?]*)? /trailing/$1?$args? break",
25152515
},
25162516
msg: "prefix path both with trailing slashes",
25172517
},

site/content/how-to/traffic-management/redirects-and-rewrites.md

+23
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ Server name: coffee-6b8b6d6486-7fc78
188188
URI: /beans
189189
```
190190

191+
```shell
192+
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee/mocha\?test\=v1\&test\=v2
193+
```
194+
195+
```text
196+
Server address: 10.244.0.235:8080
197+
Server name: coffee-6db967495b-twn6x
198+
...
199+
URI: /beans?test=v1&test=v2
200+
```
201+
191202
```shell
192203
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/latte/prices
193204
```
@@ -199,6 +210,18 @@ Server name: coffee-6b8b6d6486-7fc78
199210
URI: /prices
200211
```
201212

213+
```shell
214+
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/latte/prices\?test\=v1\&test\=v2
215+
```
216+
217+
```text
218+
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/latte/prices\?test\=v1\&test\=v2
219+
Server address: 10.244.0.235:8080
220+
Server name: coffee-6db967495b-twn6x
221+
...
222+
URI: /prices?test=v1&test=v2
223+
```
224+
202225
## Further reading
203226

204227
To learn more about redirects and rewrites using the Gateway API, see the following resource:

0 commit comments

Comments
 (0)