Dynamic forward proxy (egress)
Route arbitrary outbound HTTPS through one Envoy with DNS caching and policy.
The "egress to anywhere" problem
A normal Envoy cluster has fixed endpoints. To call api.github.com
you'd define a cluster for it. To call hooks.slack.com, another. To
let customers configure their own webhook URLs? Now you're enumerating
the open internet as Envoy clusters.
Dynamic forward proxy flips the model: the upstream host is whatever
the client sent in the :authority (HTTP Host header) or HTTP
CONNECT target. Envoy resolves it at request time, caches the result,
and routes there.
One Envoy that can talk to anything, with TLS, observability, rate limiting, and policy in front.
Minimal envoy.yaml
static_resources:
listeners:
- address: { socket_address: { address: 0.0.0.0, port_value: 10000 }}
filter_chains:
- filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
stat_prefix: egress
route_config:
virtual_hosts:
- name: any
domains: ["*"]
routes:
- match: { prefix: "/" }
route: { cluster: dfp }
http_filters:
- name: envoy.filters.http.dynamic_forward_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.dynamic_forward_proxy.v3.FilterConfig
dns_cache_config:
name: shared_cache # MUST match cluster cache
dns_lookup_family: V4_ONLY
host_ttl: 300s
max_hosts: 5000
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
clusters:
- name: dfp
lb_policy: CLUSTER_PROVIDED
cluster_type:
name: envoy.clusters.dynamic_forward_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.clusters.dynamic_forward_proxy.v3.ClusterConfig
dns_cache_config:
name: shared_cache # same name as above
dns_lookup_family: V4_ONLY
host_ttl: 300s
max_hosts: 5000
transport_socket:
name: envoy.transport_sockets.tls
typed_config:
"@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
common_tls_context:
validation_context:
trusted_ca: { filename: /etc/ssl/certs/ca-certificates.crt }
Test as a forward proxy:
curl -x http://localhost:10000 https://api.github.com/zen
Three pitfalls
host_ttl is how long Envoy caches resolutions. If
you set it to 24h and the upstream IP changes (cloud failover,
blue/green), you'll keep hitting the dead IP. Set host_ttl
close to the actual DNS TTL (often 60s) and rely on outlier detection
(Lesson 27) to evict bad endpoints faster than DNS can.
The DFP cluster auto-derives SNI from the request :authority,
so most CDNs work out of the box. But cert validation against
that hostname requires auto_san_validation: true on the
upstream TLS context — without it, any valid cert from any CA passes.
Always set this in production.
A single misbehaving upstream
(api.bad.example) can saturate connections shared across
the whole DFP cluster. Add cluster.circuit_breakers plus
outlier_detection at the cluster level — Envoy will eject
hosts from the cache when they consistently fail.
Hands-on: a working egress proxy
- Save the config above. Run Envoy:
docker run --rm -it -p 10000:10000 -p 9901:9901 \
-v "$PWD/envoy.yaml:/etc/envoy/envoy.yaml" \
envoyproxy/envoy:v1.31-latest
- Hit two unrelated external hosts:
curl -x localhost:10000 https://api.github.com/zen
curl -x localhost:10000 https://httpbin.org/ip
-
Visit
http://localhost:9901/clustersand find thedfpcluster — both hosts appear in its endpoint list, each with their own stats. -
Set
host_ttl: 10sand watchenvoy.dns_cache.shared_cache.host_addedandhost_removedcounters as entries expire and re-resolve. -
Add
outlier_detectionto the cluster. Make a request to a host returning 500s and watch it get ejected from/clusters.
What this combines with
Dynamic forward proxy slots into the same filter-chain skeleton you already know:
- JWT auth in front (so internal callers have to authenticate).
- Rate limiting per-tenant, descriptors keyed on the destination hostname.
- Access logs with
%REQ(:AUTHORITY)%so you can audit "who called what." - ext_proc to redact PII before it leaves the building.
That's a complete egress gateway in maybe 100 lines of YAML.
Why must dns_cache_config.name match between the dynamic_forward_proxy filter and the cluster?