Skip to content

Commit

Permalink
compression docs
Browse files Browse the repository at this point in the history
Signed-off-by: Huabing Zhao <[email protected]>
  • Loading branch information
zhaohuabing committed Jan 15, 2025
1 parent 18207c1 commit 98f777c
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions site/content/en/latest/tasks/traffic/response-compression.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
---
title: "Response Compression"
---

Response Compression allows you to compress the response from the backend before sending it to the client. This can be useful for scenarios where the backend sends large responses that can be compressed to reduce the network bandwidth. However, this comes with a trade-off of increased CPU usage on the Envoy side to compress the response.

## Installation

Follow the steps from the [Quickstart](../../quickstart) to install Envoy Gateway and the example manifest.
Before proceeding, you should be able to query the example backend using HTTP.

## Testing Response Compression

Envoy Gateway supports Brotli and Gzip compression algorithms. You can enable compression by specifying the compression types in the `BackendTrafficPolicy` resource. If you specify multiple compression types, Envoy will use the compression type that is requested by the client in the `Accept-Encoding` header.

{{< tabpane text=true >}}
{{% tab header="Apply from stdin" %}}

```shell
cat <<EOF | kubectl apply -f -
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
name: response-compression
spec:
targetRef:
group: gateway.networking.k8s.io
kind: HTTPRoute
name: backend
compression:
- type: Brotli
- type: Gzip
EOF
```

{{% /tab %}}
{{% tab header="Apply from file" %}}
Save and apply the following resource to your cluster:

```yaml
---
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
name: response-compression
spec:
targetRef:
group: gateway.networking.k8s.io
kind: HTTPRoute
name: backend
compression:
- type: Brotli
- type: Gzip
```
{{% /tab %}}
{{< /tabpane >}}
Send requests to the Envoy Gateway with the `Accept-Encoding` header to specify the compression type. The quantity value in the `Accept-Encoding` header is used to specify the priority of the compression type. The compression type with the highest quantity that is configured in the `BackendTrafficPolicy` resource will be used.

```shell
curl --verbose --header "Host: www.example.com" --header "Accept-Encoding: br;q=1.0, gzip;q=0.8" http://$GATEWAY_HOST/
```

```console
* Trying 172.18.0.200:80...
* Connected to 172.18.0.200 (172.18.0.200) port 80
> GET / HTTP/1.1
> Host: www.example.com
> User-Agent: curl/8.5.0
> Accept: */*
> Accept-Encoding: br;q=1.0, gzip;q=0.8
>
< HTTP/1.1 200 OK
< content-type: application/json
< x-content-type-options: nosniff
< date: Wed, 15 Jan 2025 08:23:42 GMT
< vary: Accept-Encoding
< content-encoding: br
< transfer-encoding: chunked
```

```shell
curl --verbose --header "Host: www.example.com" --header "Accept-Encoding: br;q=0.8, gzip;q=1.0" http://$GATEWAY_HOST/
```

```console
* Trying 172.18.0.200:80...
* Connected to 172.18.0.200 (172.18.0.200) port 80
> GET / HTTP/1.1
> Host: www.example.com
> User-Agent: curl/8.5.0
> Accept: */*
> Accept-Encoding: br;q=0.8, gzip;q=1.0
>
< HTTP/1.1 200 OK
< content-type: application/json
< x-content-type-options: nosniff
< date: Wed, 15 Jan 2025 08:29:22 GMT
< content-encoding: gzip
< vary: Accept-Encoding
< transfer-encoding: chunked
```

0 comments on commit 98f777c

Please sign in to comment.