|
| 1 | +--- |
| 2 | +title: "Response Compression" |
| 3 | +--- |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +Follow the steps from the [Quickstart](../../quickstart) to install Envoy Gateway and the example manifest. |
| 10 | +Before proceeding, you should be able to query the example backend using HTTP. |
| 11 | + |
| 12 | +## Testing Response Compression |
| 13 | + |
| 14 | +You can enable compression by specifying the compression types in the `BackendTrafficPolicy` resource. |
| 15 | +Multiple compression types can be defined within the resource, allowing Envoy Gateway to choose the most appropriate option based on the `Accept-Encoding header` provided by the client.. |
| 16 | + |
| 17 | +Envoy Gateway currently supports Brotli and Gzip compression algorithms. Additional compression algorithms may be supported in the future. |
| 18 | + |
| 19 | +{{< tabpane text=true >}} |
| 20 | +{{% tab header="Apply from stdin" %}} |
| 21 | + |
| 22 | +```shell |
| 23 | +cat <<EOF | kubectl apply -f - |
| 24 | +apiVersion: gateway.envoyproxy.io/v1alpha1 |
| 25 | +kind: BackendTrafficPolicy |
| 26 | +metadata: |
| 27 | + name: response-compression |
| 28 | +spec: |
| 29 | + targetRef: |
| 30 | + group: gateway.networking.k8s.io |
| 31 | + kind: HTTPRoute |
| 32 | + name: backend |
| 33 | + compression: |
| 34 | + - type: Brotli |
| 35 | + - type: Gzip |
| 36 | +EOF |
| 37 | +``` |
| 38 | + |
| 39 | +{{% /tab %}} |
| 40 | +{{% tab header="Apply from file" %}} |
| 41 | +Save and apply the following resource to your cluster: |
| 42 | + |
| 43 | +```yaml |
| 44 | +--- |
| 45 | +apiVersion: gateway.envoyproxy.io/v1alpha1 |
| 46 | +kind: BackendTrafficPolicy |
| 47 | +metadata: |
| 48 | + name: response-compression |
| 49 | +spec: |
| 50 | + targetRef: |
| 51 | + group: gateway.networking.k8s.io |
| 52 | + kind: HTTPRoute |
| 53 | + name: backend |
| 54 | + compression: |
| 55 | + - type: Brotli |
| 56 | + - type: Gzip |
| 57 | +``` |
| 58 | +
|
| 59 | +{{% /tab %}} |
| 60 | +{{< /tabpane >}} |
| 61 | +
|
| 62 | +
|
| 63 | +To specify the desired compression type, include the `Accept-Encoding` header in requests sent to the Envoy Gateway. The quality value (`q`) in the `Accept-Encoding` header determines the priority of each compression type. Envoy Gateway will select the compression type with the highest `q` value that matches a type configured in the `BackendTrafficPolicy` resource. |
| 64 | + |
| 65 | +```shell |
| 66 | +curl --verbose --header "Host: www.example.com" --header "Accept-Encoding: br;q=1.0, gzip;q=0.8" http://$GATEWAY_HOST/ |
| 67 | +``` |
| 68 | + |
| 69 | +```console |
| 70 | +* Trying 172.18.0.200:80... |
| 71 | +* Connected to 172.18.0.200 (172.18.0.200) port 80 |
| 72 | +> GET / HTTP/1.1 |
| 73 | +> Host: www.example.com |
| 74 | +> User-Agent: curl/8.5.0 |
| 75 | +> Accept: */* |
| 76 | +> Accept-Encoding: br;q=1.0, gzip;q=0.8 |
| 77 | +> |
| 78 | +< HTTP/1.1 200 OK |
| 79 | +< content-type: application/json |
| 80 | +< x-content-type-options: nosniff |
| 81 | +< date: Wed, 15 Jan 2025 08:23:42 GMT |
| 82 | +< vary: Accept-Encoding |
| 83 | +< content-encoding: br |
| 84 | +< transfer-encoding: chunked |
| 85 | +``` |
| 86 | + |
| 87 | +```shell |
| 88 | +curl --verbose --header "Host: www.example.com" --header "Accept-Encoding: br;q=0.8, gzip;q=1.0" http://$GATEWAY_HOST/ |
| 89 | +``` |
| 90 | + |
| 91 | +```console |
| 92 | +* Trying 172.18.0.200:80... |
| 93 | +* Connected to 172.18.0.200 (172.18.0.200) port 80 |
| 94 | +> GET / HTTP/1.1 |
| 95 | +> Host: www.example.com |
| 96 | +> User-Agent: curl/8.5.0 |
| 97 | +> Accept: */* |
| 98 | +> Accept-Encoding: br;q=0.8, gzip;q=1.0 |
| 99 | +> |
| 100 | +< HTTP/1.1 200 OK |
| 101 | +< content-type: application/json |
| 102 | +< x-content-type-options: nosniff |
| 103 | +< date: Wed, 15 Jan 2025 08:29:22 GMT |
| 104 | +< content-encoding: gzip |
| 105 | +< vary: Accept-Encoding |
| 106 | +< transfer-encoding: chunked |
| 107 | +``` |
0 commit comments