From f9130e5b8273222a4c0f0c4d5d62cc7e548260bc Mon Sep 17 00:00:00 2001 From: salonichf5 <146118978+salonichf5@users.noreply.github.com> Date: Mon, 6 Jan 2025 16:11:49 -0700 Subject: [PATCH] update documentation --- internal/mode/static/nginx/config/servers.go | 4 +- .../mode/static/nginx/config/servers_test.go | 8 +-- internal/mode/static/state/graph/httproute.go | 2 +- .../redirects-and-rewrites.md | 66 +++++-------------- 4 files changed, 22 insertions(+), 58 deletions(-) diff --git a/internal/mode/static/nginx/config/servers.go b/internal/mode/static/nginx/config/servers.go index 49c15bcf3f..bdc8327154 100644 --- a/internal/mode/static/nginx/config/servers.go +++ b/internal/mode/static/nginx/config/servers.go @@ -435,7 +435,7 @@ func updateLocation( location.Includes = append(location.Includes, createIncludesFromLocationSnippetsFilters(filters.SnippetsFilters)...) if filters.RequestRedirect != nil { - ret, rewrite := createReturnValForRedirectFilter(filters.RequestRedirect, listenerPort, path) + ret, rewrite := createReturnAndRewriteConfigForRedirectFilter(filters.RequestRedirect, listenerPort, path) if rewrite.MainRewrite != "" { location.Rewrites = append(location.Rewrites, rewrite.MainRewrite) } @@ -546,7 +546,7 @@ func createProxySSLVerify(v *dataplane.VerifyTLS) *http.ProxySSLVerify { } } -func createReturnValForRedirectFilter( +func createReturnAndRewriteConfigForRedirectFilter( filter *dataplane.HTTPRequestRedirectFilter, listenerPort int32, path string, diff --git a/internal/mode/static/nginx/config/servers_test.go b/internal/mode/static/nginx/config/servers_test.go index 156747e200..3975770048 100644 --- a/internal/mode/static/nginx/config/servers_test.go +++ b/internal/mode/static/nginx/config/servers_test.go @@ -999,7 +999,7 @@ func TestCreateServers(t *testing.T) { }, }, { - Path: "/redirect", + Path: "/redirect-with-path", PathType: dataplane.PathTypePrefix, MatchRules: []dataplane.MatchRule{ { @@ -1483,7 +1483,7 @@ func TestCreateServers(t *testing.T) { Includes: externalIncludes, }, { - Path: "/redirect/", + Path: "/redirect-with-path/", Type: http.ExternalLocationType, Return: &http.Return{ Code: 301, @@ -1493,7 +1493,7 @@ func TestCreateServers(t *testing.T) { Includes: externalIncludes, }, { - Path: "= /redirect", + Path: "= /redirect-with-path", Type: http.ExternalLocationType, Return: &http.Return{ Code: 301, @@ -2558,7 +2558,7 @@ func TestCreateReturnValForRedirectFilter(t *testing.T) { t.Parallel() g := NewWithT(t) - result, rewriteConfig := createReturnValForRedirectFilter(test.filter, test.listenerPort, test.path) + result, rewriteConfig := createReturnAndRewriteConfigForRedirectFilter(test.filter, test.listenerPort, test.path) g.Expect(helpers.Diff(test.expectedReturn, result)).To(BeEmpty()) g.Expect(helpers.Diff(test.expectedRewrite, rewriteConfig)).To(BeEmpty()) }) diff --git a/internal/mode/static/state/graph/httproute.go b/internal/mode/static/state/graph/httproute.go index abb1cd8806..0d9214a102 100644 --- a/internal/mode/static/state/graph/httproute.go +++ b/internal/mode/static/state/graph/httproute.go @@ -386,7 +386,7 @@ func validateFilterRewrite( default: msg := fmt.Sprintf("urlRewrite path type %s not supported", rewrite.Path.Type) valErr := field.Invalid(rewritePath.Child("path"), *rewrite.Path, msg) - return append(allErrs, valErr) + allErrs = append(allErrs, valErr) } if err := validator.ValidatePath(path); err != nil { diff --git a/site/content/how-to/traffic-management/redirects-and-rewrites.md b/site/content/how-to/traffic-management/redirects-and-rewrites.md index b7b775a2c8..cbaee03a8f 100644 --- a/site/content/how-to/traffic-management/redirects-and-rewrites.md +++ b/site/content/how-to/traffic-management/redirects-and-rewrites.md @@ -359,15 +359,17 @@ service/tea ClusterIP 10.96.151.194 80/TCP 120m ### Configure a path redirect -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: +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. -- `http://cafe.example.com/tea` to `http://cafe.example.com/organic` -- `http://cafe.example.com/tea/origin` to `http://cafe.example.com/organic/origin` +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: -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: + - `http://cafe.example.com/tea` to `http://cafe.example.com/organic` + - `http://cafe.example.com/tea/origin` to `http://cafe.example.com/organic/origin` -- `http://cafe.example.com/soda` to `http://cafe.example.com/flavors` -- `http://cafe.example.com/soda/pepsi` to `http://cafe.example.com/flavors` +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: + + - `http://cafe.example.com/soda` to `http://cafe.example.com/flavors` + - `http://cafe.example.com/soda/pepsi` to `http://cafe.example.com/flavors` To create the httproute resource, copy and paste the following into your terminal: @@ -398,25 +400,6 @@ spec: --- apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute -metadata: - name: tea -spec: - parentRefs: - - name: gateway - sectionName: http - hostnames: - - "cafe.example.com" - rules: - - matches: - - path: - type: PathPrefix - value: /organic - backendRefs: - - name: tea - port: 80 ---- -apiVersion: gateway.networking.k8s.io/v1 -kind: HTTPRoute metadata: name: soda-redirect spec: @@ -437,25 +420,6 @@ spec: type: ReplaceFullPath replaceFullPath: /flavors port: 8080 ---- -apiVersion: gateway.networking.k8s.io/v1 -kind: HTTPRoute -metadata: - name: soda -spec: - parentRefs: - - name: gateway - sectionName: http - hostnames: - - "cafe.example.com" - rules: - - matches: - - path: - type: PathPrefix - value: /flavors - backendRefs: - - name: soda - port: 80 EOF ``` @@ -463,14 +427,14 @@ EOF ### Send traffic -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. +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). {{< 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 >}} This example demonstrates a redirect from `http://cafe.example.com/tea` to `http://cafe.example.com/organic`. ```shell -curl -L --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea --include +curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea --include ``` Notice in the output that the URI has been redirected to the new location: @@ -486,7 +450,7 @@ Other examples: This example demonstrates a redirect from `http://cafe.example.com/tea/type` to `http://cafe.example.com/organic/type`. ```shell -curl -L --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea/type --include +curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea/type --include ``` ```text @@ -498,7 +462,7 @@ Location: http://cafe.example.com:8080/organic/type 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`. ```shell -curl -L --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea/type\?test\=v1 --include +curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea/type\?test\=v1 --include ``` ```text @@ -510,7 +474,7 @@ Location: http://cafe.example.com:8080/organic/type?test=v1 This example demonstrates a redirect from `http://cafe.example.com/soda` to `http://cafe.example.com/flavors`. ```shell -curl -L --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/soda --include +curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/soda --include ``` ```text @@ -522,7 +486,7 @@ Location: http://cafe.example.com:8080/flavors This example demonstrates a redirect from `http://cafe.example.com/soda/pepsi` to `http://cafe.example.com/flavors/pepsi`. ```shell -curl -L --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/soda/pepsi --include +curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/soda/pepsi --include ``` ```text @@ -534,7 +498,7 @@ Location: http://cafe.example.com:8080/flavors 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`. ```shell -curl -L --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/soda/pepsi\?test\=v1 --include +curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/soda/pepsi\?test\=v1 --include ``` ```text