Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions content/docs/traffic-management/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ weight: 400
{{< card link="route-delegation" title="Route delegation" >}}
{{< card link="buffering" title="Buffering" >}}
{{< card link="header-control" title="Header control" >}}
{{< card link="health-checks" title="Health checks" >}}
{{< card link="tcp_keepalive" title="TCP keepalive" >}}
{{< card link="transformations" title="Transformations" >}}
{{< card link="traffic-split" title="Traffic splitting" >}}
{{< /cards >}}
42 changes: 15 additions & 27 deletions content/docs/traffic-management/rewrite/host.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,17 @@ title: Host rewrites
weight: 461
description: Replace the host header value before forwarding a request to a backend service.
---
Replace the host header value before forwarding a request to a backend service.
Replace the host header value before forwarding a request to a backend service by using the `URLRewrite` filter.

For more information, see the [{{< reuse "docs/snippets/k8s-gateway-api-name.md" >}} documentation](https://gateway-api.sigs.k8s.io/api-types/httproute/#filters-optional).
For more information, see the [{{< reuse "docs/snippets/k8s-gateway-api-name.md" >}} documentation](https://gateway-api.sigs.k8s.io/reference/spec/#gateway.networking.k8s.io/v1.HTTPURLRewriteFilter).

## Before you begin

{{< reuse "docs/snippets/prereq.md" >}}

## Rewrite hosts

Path rewrites use the HTTP path modifier to rewrite <!--either an entire path or -->path prefixes.

1. Create a RouteOption resource to define your rewrite rules. In the following example the host request header is rewritten to the `www.example.com` host.
```yaml
kubectl apply -n httpbin -f- <<EOF
apiVersion: gateway.kgateway.dev/v1alpha1
kind: RouteOption
metadata:
name: rewrite
namespace: httpbin
spec:
options:
hostRewrite: 'www.example.com'
EOF
```

2. Create an HTTPRoute resource for the httpbin app that references the RouteOption resource that you created. In this example, all incoming requests on the `rewrite.example` domain are rewritten to the `www.example.com` host as defined in the referenced RouteOption resource.
1. Create an HTTPRoute resource for the httpbin app that uses the `URLRewrite` filter to rewrite the hostname of th request. In this example, all incoming requests on the `rewrite.example` domain are rewritten to the `www.example.com` host.
```yaml
kubectl apply -f- <<EOF
apiVersion: gateway.networking.k8s.io/v1
Expand All @@ -45,18 +29,23 @@ Path rewrites use the HTTP path modifier to rewrite <!--either an entire path or
- rewrite.example
rules:
- filters:
- type: ExtensionRef
extensionRef:
group: gateway.solo.io
kind: RouteOption
name: rewrite
- type: URLRewrite
urlRewrite:
hostname: "www.example.com"
backendRefs:
- name: httpbin
port: 8000
EOF
```

|Setting|Description|
|--|--|
|`spec.parentRefs`| The name and namespace of the Gateway that serves this HTTPRoute. In this example, you use the `http` gateway that was created as part of the get started guide. |
|`spec.rules.filters.type`| The type of filter that you want to apply to incoming requests. In this example, the `URLRewrite` filter is used.|
|`spec.rules.filters.urlRewrite.hostname`| The hostname that you want to rewrite requests to. |
|`spec.rules.backendRefs`|The backend destination you want to forward traffic to. In this example, all traffic is forwarded to the httpbin app that you set up as part of the get started guide. |

3. Send a request to the httpbin app on the `rewrite.example` domain. Verify that you get back a 200 HTTP response code and that you see the `Host: www.example.com` header in your response.
2. Send a request to the httpbin app on the `rewrite.example` domain. Verify that you get back a 200 HTTP response code and that you see the `Host: www.example.com` header in your response.

{{< callout type="info" >}}
The following request returns a 200 HTTP response code, because you set up an HTTPRoute for the httpbin app on the `www.example.com` domain as part of the [Getting started guide](/docs/quickstart/). If you chose a different domain for your example, make sure that you have an HTTPRoute that can be reached under the host you want to rewrite to.
Expand Down Expand Up @@ -102,8 +91,7 @@ Path rewrites use the HTTP path modifier to rewrite <!--either an entire path or
}
```

4. Optional: Clean up the resources that you created.
3. Optional: Clean up the resources that you created.
```sh
kubectl delete routeoption rewrite -n httpbin
kubectl delete httproute httpbin-rewrite -n httpbin
```
151 changes: 97 additions & 54 deletions content/docs/traffic-management/rewrite/path.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,24 @@ weight: 462
description: Rewrite path prefixes in requests.
---

Rewrite path prefixes in requests.
Rewrite path prefixes in requests by using the `URLRewrite` filter.

For more information, see the [{{< reuse "docs/snippets/k8s-gateway-api-name.md" >}} documentation](https://gateway-api.sigs.k8s.io/api-types/httproute/#filters-optional).
For more information, see the [{{< reuse "docs/snippets/k8s-gateway-api-name.md" >}} documentation](https://gateway-api.sigs.k8s.io/reference/spec/#gateway.networking.k8s.io/v1.HTTPURLRewriteFilter).

## Before you begin

{{< reuse "docs/snippets/prereq.md" >}}

## Rewrite prefix path

Path rewrites use the HTTP path modifier to rewrite <!--either an entire path or -->path prefixes.
Path rewrites use the [HTTPPathModifier](https://gateway-api.sigs.k8s.io/reference/spec/#gateway.networking.k8s.io/v1.HTTPPathModifierType) to rewrite either an entire path or path prefixes.

1. Create a RouteOption resource to define your rewrite rules. In the following example all incoming request paths are rewritten to the `/anything` path.
```yaml
kubectl apply -n httpbin -f- <<EOF
apiVersion: gateway.kgateway.dev/v1alpha1
kind: RouteOption
metadata:
name: rewrite
namespace: httpbin
spec:
options:
prefixRewrite: '/anything'
EOF
```
### Rewrite prefix path

2. Create an HTTPRoute resource for the httpbin app that references the RouteOption resource that you created. In this example, all incoming requests that match the `/headers` path on the `rewrite.example` domain are rewritten according to the rules that are defined in the RouteOption resource.
1. Create an HTTPRoute resource for the httpbin app that configures an `URLRewrite` filter to rewrite prefix paths. In this example, all incoming requests that match the `/headers` prefix path on the `rewrite.example` domain are rewritten to the `/anything` prefix path.

Because the `ReplacePrefixPath` path modifier is used, only the path prefix is replaced during the rewrite. For example, requests to `http://rewrite.example/headers` are rewritten to `https://rewrite.example/anything`. However, for longer paths, such as in `http://rewrite.example/headers/200`, only the prefix is replaced and the path is rewritten to `http://rewrite.example/anything/200`.

```yaml
kubectl apply -f- <<EOF
apiVersion: gateway.networking.k8s.io/v1
Expand All @@ -47,21 +38,29 @@ Path rewrites use the HTTP path modifier to rewrite <!--either an entire path or
rules:
- matches:
- path:
type: Exact
type: PathPrefix
value: /headers
filters:
- type: ExtensionRef
extensionRef:
group: gateway.solo.io
kind: RouteOption
name: rewrite
- type: URLRewrite
urlRewrite:
path:
type: ReplacePrefixMatch
replacePrefixMatch: /anything
backendRefs:
- name: httpbin
port: 8000
EOF
```

|Setting|Description|
|--|--|
|`spec.parentRefs`| The name and namespace of the Gateway that serves this HTTPRoute. In this example, you use the `http` gateway that was created as part of the get started guide. |
|`spec.rules.filters.type`| The type of filter that you want to apply to incoming requests. In this example, the `URLRewrite` filter is used.|
|`spec.rules.filters.urlRewrite.path.type`| The type of HTTPPathModifier that you want to use. In this example, `ReplacePrefixMatch` is used, which replaces only the path prefix. |
| `spec.rules.filters.urlRewrite.path.replacePrefixMatch` | The path prefix you want to rewrite to. In this example, you replace the prefix path with the `/anything` prefix path. |
|`spec.rules.backendRefs`|The backend destination you want to forward traffic to. In this example, all traffic is forwarded to the httpbin app that you set up as part of the get started guide. |

3. Send a request to the httpbin app along the `/headers` path on the `rewrite.example` domain. Verify that you get back a 200 HTTP response code and that your request is rewritten to the `/anything` path.
2. Send a request to the httpbin app along the `/headers` path on the `rewrite.example` domain. Verify that you get back a 200 HTTP response code and that your request is rewritten to the `/anything` path.
{{< tabs items="Cloud Provider LoadBalancer,Port-forward for local testing" >}}
{{% tab %}}
```sh
Expand All @@ -87,32 +86,43 @@ Path rewrites use the HTTP path modifier to rewrite <!--either an entire path or
...
```

4. Optional: Clean up the resources that you created.
3. Send another request to the httpbin app. This time, you send it along the `/headers/200` path on the `rewrite.example` domain. Verify that you get back a 200 HTTP response code and that your request path is rewritten to `/anything/200`.
{{< tabs items="Cloud Provider LoadBalancer,Port-forward for local testing" >}}
{{% tab %}}
```sh
kubectl delete routeoption rewrite -n httpbin
kubectl delete httproute httpbin-rewrite -n httpbin
curl -vik http://$INGRESS_GW_ADDRESS:8080/headers/200 -H "host: rewrite.example:8080"
```
{{% /tab %}}
{{% tab %}}
```sh
curl -vik localhost:8080/headers/200 -H "host: rewrite.example"
```
{{% /tab %}}
{{< /tabs >}}

Example output:
```
...
"origin": "10.0.9.36:50660",
"url": "http://rewrite.example:8080/anything/200",
"data": "",
"files": null,
"form": null,
"json": null
...
```

## Rewrite full or prefix path with regex

1. Create a RouteOption resource to define your rewrite rules. In the following example all incoming request paths are evaluated against the regex pattern. If `headers` is part of the request path, it is replaced with `anything`.
```yaml
kubectl apply -n httpbin -f- <<EOF
apiVersion: gateway.kgateway.dev/v1alpha1
kind: RouteOption
metadata:
name: rewrite
namespace: httpbin
spec:
options:
regexRewrite:
pattern:
regex: 'headers'
substitution: 'anything'
EOF
4. Optional: Clean up the HTTPRoute that you created.
```sh
kubectl delete httproute httpbin-rewrite -n httpbin
```

2. Create an HTTPRoute resource for the httpbin app that references the RouteOption resource that you created. In this example, all incoming requests along the `/headers` path on the `rewrite.example` domain are evaluated against the regex pattern that you defined in the RouteOption resource.
### Rewrite full path

1. Create an HTTPRoute resource for the httpbin app that configures an `URLRewrite` filter to rewrite prefix paths. In this example, all incoming requests that match the `/headers` prefix path on the `rewrite.example` domain are rewritten to `/anything`.

Because the `ReplaceFullPath` path modifier is used, requests to `http://rewrite.example/headers` and `http://rewrite.example/headers/200` both are rewritten to `https://rewrite.example/anything`.

```yaml
kubectl apply -f- <<EOF
apiVersion: gateway.networking.k8s.io/v1
Expand All @@ -129,20 +139,28 @@ Path rewrites use the HTTP path modifier to rewrite <!--either an entire path or
rules:
- matches:
- path:
type: Exact
type: PathPrefix
value: /headers
filters:
- type: ExtensionRef
extensionRef:
group: gateway.solo.io
kind: RouteOption
name: rewrite
- type: URLRewrite
urlRewrite:
path:
type: ReplaceFullPath
replaceFullPath: /anything
backendRefs:
- name: httpbin
port: 8000
EOF
```

|Setting|Description|
|--|--|
|`spec.parentRefs`| The name and namespace of the Gateway that serves this HTTPRoute. In this example, you use the `http` gateway that was created as part of the get started guide. |
|`spec.rules.filters.type`| The type of filter that you want to apply to incoming requests. In this example, the `URLRewrite` filter is used.|
|`spec.rules.filters.urlRewrite.path.type`| The type of HTTPPathModifier that you want to use. In this example, `ReplaceFullPath` is used, which replaces the full path prefix. |
| `spec.rules.filters.urlRewrite.path.replaceFullPath` | The path prefix you want to rewrite to. In this example, you replace the full prefix path with the `/anything` prefix path. |
|`spec.rules.backendRefs`|The backend destination you want to forward traffic to. In this example, all traffic is forwarded to the httpbin app that you set up as part of the get started guide. |

3. Send a request to the httpbin app along the `/headers` path on the `rewrite.example` domain. Verify that you get back a 200 HTTP response code and that your request is rewritten to the `/anything` path.
{{< tabs items="Cloud Provider LoadBalancer,Port-forward for local testing" >}}
{{% tab %}}
Expand All @@ -156,7 +174,33 @@ Path rewrites use the HTTP path modifier to rewrite <!--either an entire path or
```
{{% /tab %}}
{{< /tabs >}}

Example output:
```
...
"origin": "10.0.9.36:50660",
"url": "http://rewrite.example:8080/anything",
"data": "",
"files": null,
"form": null,
"json": null
...
```

4. Send another request to the httpbin app. This time, you send it along the `/headers/200` path on the `rewrite.example` domain. Verify that you also get back a 200 HTTP response code and that the full path is rewritten to the `/anything` path.
{{< tabs items="Cloud Provider LoadBalancer,Port-forward for local testing" >}}
{{% tab %}}
```sh
curl -vik http://$INGRESS_GW_ADDRESS:8080/headers/200 -H "host: rewrite.example:8080"
```
{{% /tab %}}
{{% tab %}}
```sh
curl -vik localhost:8080/headers/200 -H "host: rewrite.example"
```
{{% /tab %}}
{{< /tabs >}}

Example output:
```
...
Expand All @@ -169,8 +213,7 @@ Path rewrites use the HTTP path modifier to rewrite <!--either an entire path or
...
```

4. Optional: Clean up the resources that you created.
5. Optional: Clean up the HTTPRoute that you created.
```sh
kubectl delete routeoption rewrite -n httpbin
kubectl delete httproute httpbin-rewrite -n httpbin
```
```