Compress Response
The compress-response
Traffic Policy action enables you to improve the
performance of your web applications by compressing HTTP response bodies returned
by your upstream service.
Supported Phases
on_http_request
, on_http_response
Type
compress-response
Configuration Fields
algorithmsarray of strings
A list of allowed compression algorithms to be considered during encoding type negotiation. Each element must be unique.
Defaults to
gzip
,deflate
,br
,compress
.- Supported algorithms
br
compress
deflate
gzip
Behavior
When an HTTP request includes an Accept-Encoding
header, HTTP responses are
automatically compressed, and a Content-Encoding header is added to the
response.
If the response is already compressed by your upstream service, no additional compression will be applied.
Streaming Compression
When compression is applied, the response body is not buffered; it is compressed in real-time as it streams through the ngrok endpoint.
Algorithm Choice
If a request specifies Accept-Encoding
but none of the requested algorithms
are supported, no compression is applied, and the upstream response is returned
unmodified.
Quality Values
Compression is negotiated based on the Accept-Encoding
header as defined by
the RFC,
respecting q-values and selecting the supported algorithm with the highest
q-value.
For example:
Loading…
The algorithm gzip
would be selected because it has the highest q-value of 1.0
.
Response Headers
When this action performs compression, the following changes are made to the HTTP response headers:
- The
Content-Length
header is removed. - A
Content-Encoding
header is added, specifying the negotiated algorithm. - A
Vary: Accept-Encoding
header is added to indicate that the response varies based on theAccept-Encoding
request header.
Example Compressing API Responses Based on URL Path
The following Traffic Policy
configuration demonstrates how to set this up using the compress-response
action with an expression to compress traffic on specific URL paths.
Traffic Policy Document
You can use one of the following snippets:
Policy
- YAML
- JSON
Loading…
Loading…
Agent Config
- YAML
- JSON
Loading…
Loading…
For this example, we are assuming that an ngrok agent is pointing at the upstream service https://jsonplaceholder.typicode.com.
Request
Loading…
Loading…
In this example, when a request is made to /api/posts
,
ngrok compresses the response using one of the specified algorithms
(e.g., gzip
). The response includes the content-encoding: gzip
header, and
the body is compressed accordingly.
This setup ensures that only API responses
for paths starting with /api/
are compressed, enhancing performance and
responsiveness for only those endpoints.
Action Result Variables
actions.ngrok.compress.already_compressedboolean
Indicates whether the body was already compressed before the action was applied. Returns
true
if no further compression was performed.actions.ngrok.compress.negotiated_algorithmstring
The compression algorithm selected and applied by the action, based on the client's request and action configuration.