You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
363
363
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:
366
365
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`
368
368
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`
371
373
372
374
To create the httproute resource, copy and paste the following into your terminal:
373
375
@@ -398,25 +400,6 @@ spec:
398
400
---
399
401
apiVersion: gateway.networking.k8s.io/v1
400
402
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
420
403
metadata:
421
404
name: soda-redirect
422
405
spec:
@@ -437,40 +420,21 @@ spec:
437
420
type: ReplaceFullPath
438
421
replaceFullPath: /flavors
439
422
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
459
423
EOF
460
424
```
461
425
462
426
---
463
427
464
428
### Send traffic
465
429
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).
467
431
468
432
{{< 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 >}}
469
433
470
434
This example demonstrates a redirect from `http://cafe.example.com/tea` to `http://cafe.example.com/organic`.
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`.
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`.
0 commit comments