Documentation Index
Fetch the complete documentation index at: https://ngrok.com/docs/llms.txt
Use this file to discover all available pages before exploring further.
Response compression reduces the size of HTTP response bodies sent from your upstream service, improving page load speeds, reducing bandwidth usage, and enhancing user experience.
When response compression is enabled:
📉 Data transfer sizes are reduced, lowering bandwidth consumption.
⚡ Pages and APIs load faster, especially on slower networks.
🚀 Performance is improved without requiring changes to the upstream service.
🔍 What are the benefits of response compression?
Every millisecond counts when serving web applications and APIs.
Compression is a simple yet effective way to improve performance without modifying backend code.
Response compression offers the following benefits:
- Faster Web & API Performance. Users receive responses quicker, reducing page load times.
- Compress JSON and XML payloads for faster API calls.
- Lower Bandwidth Usage. Ideal for high-traffic APIs, mobile users, and global applications.
- Better Client Experience. Reduces latency, especially in areas with slower network connections.
- Reduces bandwidth usage for users on slow or metered connections.
- No Backend Changes Needed. Compression is handled at the Traffic Policy level, requiring zero code modifications.
Response compression examples
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.
The following examples configure the endpoint to have ngrok compress the response using one of the specified algorithms (for example, gzip).
The response will include the content-encoding: gzip header, and the body will be compressed accordingly.
The Traffic Policy includes an expression to only run the compression on paths that start with /api/
Check out the compress response Traffic Policy action page for more details about how it functions and the parameters it accepts.
AgentEndpoint
CloudEndpoint
Ingress
Gateway API
apiVersion: ngrok.k8s.ngrok.com/v1alpha1
kind: AgentEndpoint
metadata:
name: example-agent-endpoint
spec:
url: https://example-hostname.ngrok.io
upstream:
url: http://my-service.my-namespace:8080
trafficPolicy:
inline:
on_http_response:
- expressions:
- req.url.path.startsWith('/api/')
actions:
- type: compress-response
config:
algorithms:
- gzip
- br
- deflate
- compress
apiVersion: ngrok.k8s.ngrok.com/v1alpha1
kind: CloudEndpoint
metadata:
name: example-cloud-endpoint
spec:
url: https://example-hostname.ngrok.io
trafficPolicy:
policy:
on_http_response:
- expressions:
- req.url.path.startsWith('/api/')
actions:
- type: compress-response
config:
algorithms:
- gzip
- br
- deflate
- compress
💡 Ingress resources do not natively support response compression, but they can be extended using a Traffic Policy.1. Create an NgrokTrafficPolicy
apiVersion: ngrok.k8s.ngrok.com/v1alpha1
kind: NgrokTrafficPolicy
metadata:
name: example-tp
namespace: default
spec:
policy:
on_http_response:
- expressions:
- req.url.path.startsWith('/api/')
actions:
- type: compress-response
config:
algorithms:
- gzip
- br
- deflate
- compress
2. Use the NgrokTrafficPolicy on an Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
k8s.ngrok.com/traffic-policy: example-tp
name: example-ingress
namespace: default
spec:
ingressClassName: ngrok
rules:
- host: example-hostname.ngrok.io
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: example-service
port:
number: 80
💡 Gateway API resources do not natively support response compression, but they can be extended using a Traffic Policy.1. Create an NgrokTrafficPolicy
apiVersion: ngrok.k8s.ngrok.com/v1alpha1
kind: NgrokTrafficPolicy
metadata:
name: example-tp
namespace: default
spec:
policy:
on_http_response:
- expressions:
- req.url.path.startsWith('/api/')
actions:
- type: compress-response
config:
algorithms:
- gzip
- br
- deflate
- compress
2. Use the NgrokTrafficPolicy on a Gateway
The following example showcases supplying the NgrokTrafficPolicy on a Gateway resource. All requests to the Gateway will run the Traffic Policy.
If you prefer, NgrokTrafficPolicy can also be used on the route level by using an externalRef filter on an HTTPRoute. See the using Gateway API guide for examples.apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: example-gateway
namespace: default
annotations:
k8s.ngrok.com/traffic-policy: example-tp
spec:
gatewayClassName: ngrok
listeners:
- name: example-hostname
hostname: "example-hostname.ngrok.io"
port: 443
protocol: HTTPS