envoy-guide
Module 7 · Going Deeper·12 min·intermediate

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

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:

Shell
curl -x http://localhost:10000 https://api.github.com/zen

Three pitfalls

host_ttl vs DNS TTL drift

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.

SNI is set; SAN validation isn't

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.

No per-host circuit breaking

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

Hands-on · Proxy two hosts through one Envoy
  1. Save the config above. Run Envoy:
Shell
docker run --rm -it -p 10000:10000 -p 9901:9901 \
  -v "$PWD/envoy.yaml:/etc/envoy/envoy.yaml" \
  envoyproxy/envoy:v1.31-latest
  1. Hit two unrelated external hosts:
Shell
curl -x localhost:10000 https://api.github.com/zen
curl -x localhost:10000 https://httpbin.org/ip
  1. Visit http://localhost:9901/clusters and find the dfp cluster — both hosts appear in its endpoint list, each with their own stats.

  2. Set host_ttl: 10s and watch envoy.dns_cache.shared_cache.host_added and host_removed counters as entries expire and re-resolve.

  3. Add outlier_detection to the cluster. Make a request to a host returning 500s and watch it get ejected from /clusters.

Real-world example · Cardinal SaaS (configurable webhooks)
Problem
Cardinal lets customers configure outbound webhooks to *their own* URLs. Marketing keeps adding new integrations — Salesforce, custom partner endpoints, ad-hoc HTTPS callbacks. Pre-Envoy, every new integration meant adding a cluster and redeploying.
Envoy solution
One Envoy with dynamic_forward_proxy in front of all egress, attached to JWT-based per-tenant rate limiting and audit logging. Adding a new outbound destination is now a config change in their app, not in the proxy. The proxy never changes.

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.

Quick check

Why must dns_cache_config.name match between the dynamic_forward_proxy filter and the cluster?

Practical task
Finish the quick check and tick off the task to unlock.
Quick checkTasks 0/3