Skip to content

Commit f9130e5

Browse files
committed
update documentation
1 parent 17012b4 commit f9130e5

File tree

4 files changed

+22
-58
lines changed

4 files changed

+22
-58
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ func updateLocation(
435435
location.Includes = append(location.Includes, createIncludesFromLocationSnippetsFilters(filters.SnippetsFilters)...)
436436

437437
if filters.RequestRedirect != nil {
438-
ret, rewrite := createReturnValForRedirectFilter(filters.RequestRedirect, listenerPort, path)
438+
ret, rewrite := createReturnAndRewriteConfigForRedirectFilter(filters.RequestRedirect, listenerPort, path)
439439
if rewrite.MainRewrite != "" {
440440
location.Rewrites = append(location.Rewrites, rewrite.MainRewrite)
441441
}
@@ -546,7 +546,7 @@ func createProxySSLVerify(v *dataplane.VerifyTLS) *http.ProxySSLVerify {
546546
}
547547
}
548548

549-
func createReturnValForRedirectFilter(
549+
func createReturnAndRewriteConfigForRedirectFilter(
550550
filter *dataplane.HTTPRequestRedirectFilter,
551551
listenerPort int32,
552552
path string,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,7 @@ func TestCreateServers(t *testing.T) {
999999
},
10001000
},
10011001
{
1002-
Path: "/redirect",
1002+
Path: "/redirect-with-path",
10031003
PathType: dataplane.PathTypePrefix,
10041004
MatchRules: []dataplane.MatchRule{
10051005
{
@@ -1483,7 +1483,7 @@ func TestCreateServers(t *testing.T) {
14831483
Includes: externalIncludes,
14841484
},
14851485
{
1486-
Path: "/redirect/",
1486+
Path: "/redirect-with-path/",
14871487
Type: http.ExternalLocationType,
14881488
Return: &http.Return{
14891489
Code: 301,
@@ -1493,7 +1493,7 @@ func TestCreateServers(t *testing.T) {
14931493
Includes: externalIncludes,
14941494
},
14951495
{
1496-
Path: "= /redirect",
1496+
Path: "= /redirect-with-path",
14971497
Type: http.ExternalLocationType,
14981498
Return: &http.Return{
14991499
Code: 301,
@@ -2558,7 +2558,7 @@ func TestCreateReturnValForRedirectFilter(t *testing.T) {
25582558
t.Parallel()
25592559
g := NewWithT(t)
25602560

2561-
result, rewriteConfig := createReturnValForRedirectFilter(test.filter, test.listenerPort, test.path)
2561+
result, rewriteConfig := createReturnAndRewriteConfigForRedirectFilter(test.filter, test.listenerPort, test.path)
25622562
g.Expect(helpers.Diff(test.expectedReturn, result)).To(BeEmpty())
25632563
g.Expect(helpers.Diff(test.expectedRewrite, rewriteConfig)).To(BeEmpty())
25642564
})

internal/mode/static/state/graph/httproute.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ func validateFilterRewrite(
386386
default:
387387
msg := fmt.Sprintf("urlRewrite path type %s not supported", rewrite.Path.Type)
388388
valErr := field.Invalid(rewritePath.Child("path"), *rewrite.Path, msg)
389-
return append(allErrs, valErr)
389+
allErrs = append(allErrs, valErr)
390390
}
391391

392392
if err := validator.ValidatePath(path); err != nil {

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

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -359,15 +359,17 @@ service/tea ClusterIP 10.96.151.194 <none> 80/TCP 120m
359359

360360
### Configure a path redirect
361361

362-
We will define two HTTPRoutes for the **tea** application: `tea`, which specifies the destination to handle redirected requests, and `tea-redirect` that redirect requests as follows:
362+
In this section, we'll define two HTTPRoutes for the tea and soda applications to demonstrate different types of request redirection using the `RequestRedirect` filter.
363363

364-
- `http://cafe.example.com/tea` to `http://cafe.example.com/organic`
365-
- `http://cafe.example.com/tea/origin` to `http://cafe.example.com/organic/origin`
364+
1. The `tea-redirect` route uses the `ReplacePrefixMatch` type for path modification. This configuration matches the prefix of the original path and updates it to a new path, preserving the rest of the original URL structure. It will redirect request as follows:
366365

367-
We will also define two HTTPRoutes defined for the **soda** application: `soda`, which specifies the destination to handle redirected requests, and `soda-redirect` that redirect requests as follows:
366+
- `http://cafe.example.com/tea` to `http://cafe.example.com/organic`
367+
- `http://cafe.example.com/tea/origin` to `http://cafe.example.com/organic/origin`
368368

369-
- `http://cafe.example.com/soda` to `http://cafe.example.com/flavors`
370-
- `http://cafe.example.com/soda/pepsi` to `http://cafe.example.com/flavors`
369+
2. The `soda-redirect` route uses the `ReplaceFullPath` type for path modification. This configuration updates the entire original path to the new location, effectively overwriting it. It will redirect request as follows:
370+
371+
- `http://cafe.example.com/soda` to `http://cafe.example.com/flavors`
372+
- `http://cafe.example.com/soda/pepsi` to `http://cafe.example.com/flavors`
371373

372374
To create the httproute resource, copy and paste the following into your terminal:
373375

@@ -398,25 +400,6 @@ spec:
398400
---
399401
apiVersion: gateway.networking.k8s.io/v1
400402
kind: HTTPRoute
401-
metadata:
402-
name: tea
403-
spec:
404-
parentRefs:
405-
- name: gateway
406-
sectionName: http
407-
hostnames:
408-
- "cafe.example.com"
409-
rules:
410-
- matches:
411-
- path:
412-
type: PathPrefix
413-
value: /organic
414-
backendRefs:
415-
- name: tea
416-
port: 80
417-
---
418-
apiVersion: gateway.networking.k8s.io/v1
419-
kind: HTTPRoute
420403
metadata:
421404
name: soda-redirect
422405
spec:
@@ -437,40 +420,21 @@ spec:
437420
type: ReplaceFullPath
438421
replaceFullPath: /flavors
439422
port: 8080
440-
---
441-
apiVersion: gateway.networking.k8s.io/v1
442-
kind: HTTPRoute
443-
metadata:
444-
name: soda
445-
spec:
446-
parentRefs:
447-
- name: gateway
448-
sectionName: http
449-
hostnames:
450-
- "cafe.example.com"
451-
rules:
452-
- matches:
453-
- path:
454-
type: PathPrefix
455-
value: /flavors
456-
backendRefs:
457-
- name: soda
458-
port: 80
459423
EOF
460424
```
461425

462426
---
463427

464428
### Send traffic
465429

466-
Using the external IP address and port for NGINX Gateway Fabric, we can send traffic to our tea and soda applications to verify the redirect is successful. We will use curl's `--include` option to print the response headers (we are interested in the `Location` header) and `-L` to follow redirects, ensuring that the user fetches the final destination after encountering HTTP 3xx redirect response.
430+
Using the external IP address and port for NGINX Gateway Fabric, we can send traffic to our tea and soda applications to verify the redirect is successful. We will use curl's `--include` option to print the response headers (we are interested in the `Location` header).
467431

468432
{{< note >}}If you have a DNS record allocated for `cafe.example.com`, you can send the request directly to that hostname, without needing to resolve.{{< /note >}}
469433

470434
This example demonstrates a redirect from `http://cafe.example.com/tea` to `http://cafe.example.com/organic`.
471435

472436
```shell
473-
curl -L --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea --include
437+
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea --include
474438
```
475439

476440
Notice in the output that the URI has been redirected to the new location:
@@ -486,7 +450,7 @@ Other examples:
486450
This example demonstrates a redirect from `http://cafe.example.com/tea/type` to `http://cafe.example.com/organic/type`.
487451

488452
```shell
489-
curl -L --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea/type --include
453+
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea/type --include
490454
```
491455

492456
```text
@@ -498,7 +462,7 @@ Location: http://cafe.example.com:8080/organic/type
498462
This example demonstrates a redirect from `http://cafe.example.com/tea/type` to `http://cafe.example.com/organic/type` and specifies query params `test=v1`.
499463

500464
```shell
501-
curl -L --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea/type\?test\=v1 --include
465+
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea/type\?test\=v1 --include
502466
```
503467

504468
```text
@@ -510,7 +474,7 @@ Location: http://cafe.example.com:8080/organic/type?test=v1
510474
This example demonstrates a redirect from `http://cafe.example.com/soda` to `http://cafe.example.com/flavors`.
511475

512476
```shell
513-
curl -L --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/soda --include
477+
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/soda --include
514478
```
515479

516480
```text
@@ -522,7 +486,7 @@ Location: http://cafe.example.com:8080/flavors
522486
This example demonstrates a redirect from `http://cafe.example.com/soda/pepsi` to `http://cafe.example.com/flavors/pepsi`.
523487

524488
```shell
525-
curl -L --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/soda/pepsi --include
489+
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/soda/pepsi --include
526490
```
527491

528492
```text
@@ -534,7 +498,7 @@ Location: http://cafe.example.com:8080/flavors
534498
This example demonstrates a redirect from `http://cafe.example.com/soda/pepsi` to `http://cafe.example.com/flavors/pepsi` and specifies query params `test=v1`.
535499

536500
```shell
537-
curl -L --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/soda/pepsi\?test\=v1 --include
501+
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/soda/pepsi\?test\=v1 --include
538502
```
539503

540504
```text

0 commit comments

Comments
 (0)