Skip to content

Commit

Permalink
update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
salonichf5 committed Jan 7, 2025
1 parent 17012b4 commit 2d20bed
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 59 deletions.
4 changes: 2 additions & 2 deletions internal/mode/static/nginx/config/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -546,7 +546,7 @@ func createProxySSLVerify(v *dataplane.VerifyTLS) *http.ProxySSLVerify {
}
}

func createReturnValForRedirectFilter(
func createReturnAndRewriteConfigForRedirectFilter(
filter *dataplane.HTTPRequestRedirectFilter,
listenerPort int32,
path string,
Expand Down
8 changes: 4 additions & 4 deletions internal/mode/static/nginx/config/servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ func TestCreateServers(t *testing.T) {
},
},
{
Path: "/redirect",
Path: "/redirect-with-path",
PathType: dataplane.PathTypePrefix,
MatchRules: []dataplane.MatchRule{
{
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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())
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type HTTPURLRewriteValidator struct{}
// which in NGINX is done with the proxy_set_header directive.
type HTTPHeaderValidator struct{}

// HTTPCommonValidator validates values for path used in filters.
// HTTPPathValidator validates values for path used in filters.
type HTTPPathValidator struct{}

var supportedRedirectSchemes = map[string]struct{}{
Expand Down
2 changes: 1 addition & 1 deletion internal/mode/static/state/graph/httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
66 changes: 15 additions & 51 deletions site/content/how-to/traffic-management/redirects-and-rewrites.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,17 @@ service/tea ClusterIP 10.96.151.194 <none> 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:

Expand Down Expand Up @@ -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:
Expand All @@ -437,40 +420,21 @@ 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
```

---

### 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:
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 2d20bed

Please sign in to comment.