envoy-guide
Module 6 · Real-world Recipes·14 min·intermediate

gRPC routing and gRPC-Web

Proxy native gRPC and translate browser gRPC-Web at the edge.

gRPC is just HTTP/2 (with conventions)

A gRPC call is a POST to /<package>.<Service>/<Method> with content-type: application/grpc. The response body is a length-prefixed protobuf, and the real status lives in trailers (grpc-status, grpc-message), not the HTTP status line.

That means Envoy can route gRPC the same way it routes any other HTTP request — as long as you've turned on HTTP/2 on both sides.

The one-line bug everyone hits

The cluster MUST opt into HTTP/2.

Without that, Envoy downgrades the upstream connection to HTTP/1.1 and your gRPC backend rejects it with the world's most confusing error:

Text
upstream connect error or disconnect/reset before headers

The fix lives in typed_extension_protocol_options on the cluster:

YAML
  clusters:
  - name: greeter
    type: STRICT_DNS
    connect_timeout: 1s
    typed_extension_protocol_options:
      envoy.extensions.upstreams.http.v3.HttpProtocolOptions:
        "@type": type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions
        explicit_http_config:
          http2_protocol_options: {}
    load_assignment:
      cluster_name: greeter
      endpoints:
      - lb_endpoints:
        - endpoint: { address: { socket_address: { address: greeter, port_value: 50051 }}}
Old field names will lie to you

Pre-1.21 docs and Stack Overflow answers show http2_protocol_options directly on the cluster. That's deprecated. Use typed_extension_protocol_options as above. Both seem to work until they don't.

A complete, runnable Envoy config

YAML
static_resources:
  listeners:
  - name: grpc_listener
    address: { socket_address: { address: 0.0.0.0, port_value: 8080 }}
    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
          codec_type: AUTO
          stat_prefix: grpc_proxy
          http2_protocol_options: {}            # accept h2c from clients
          route_config:
            virtual_hosts:
            - name: grpc_vh
              domains: ["*"]
              routes:
              # Route an entire service:
              - match: { prefix: "/helloworld.Greeter/" }
                route: { cluster: greeter, timeout: 0s }
          http_filters:
          - name: envoy.filters.http.grpc_web
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.filters.http.grpc_web.v3.GrpcWeb
          - name: envoy.filters.http.router
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router

Two things worth calling out:

  1. timeout: 0s on streaming RPCs. Server-streaming and bidi calls live for a long time; a finite request timeout will cut them off mid- stream. Use idle_timeout on the HCM instead if you need protection.
  2. grpc_web filter lets browsers in. More on that below.

Routing by service or method

The path is always /<fully.qualified.Service>/<Method>:

YAML
              routes:
              - match: { prefix: "/payments.Charge/" }
                route: { cluster: charge_cluster }
              - match: { path: "/payments.Charge/Refund" }
                route: { cluster: refunds_special_cluster }   # one method only

That second route is genuinely useful: send heavy or slow methods to a beefier cluster without touching the lighter ones.

gRPC-Web for browsers

Browsers can't speak raw gRPC — the Fetch API doesn't expose HTTP/2 trailers. The envoy.filters.http.grpc_web filter handles the impedance mismatch: clients send gRPC-Web (a base64-framed variant); Envoy unwraps it into standard gRPC and forwards.

You'll usually also want CORS — add envoy.filters.http.cors before the router and a cors policy on the route. Otherwise browsers refuse the preflight.

Hands-on: proxy a real gRPC service

Hands-on · grpcurl through Envoy

Use the public gRPC reflection demo at grpcb.in:

  1. Save the Envoy config above with a single change — the cluster points at grpcb.in:443 with TLS:
YAML
  clusters:
  - name: greeter
    type: LOGICAL_DNS
    connect_timeout: 2s
    transport_socket:
      name: envoy.transport_sockets.tls
      typed_config:
        "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
        sni: grpcb.in
    typed_extension_protocol_options:
      envoy.extensions.upstreams.http.v3.HttpProtocolOptions:
        "@type": type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions
        explicit_http_config: { http2_protocol_options: {} }
    load_assignment:
      cluster_name: greeter
      endpoints:
      - lb_endpoints:
        - endpoint: { address: { socket_address: { address: grpcb.in, port_value: 9001 }}}
  1. Run Envoy as usual.

  2. Install grpcurl and call through:

Shell
grpcurl -plaintext -d '{"greeting":"hi"}' localhost:8080 \
  hello.HelloService/SayHello

You should get a JSON response back. If you instead see "upstream connect error", you know what to check first: http2_protocol_options on the cluster.

Log gRPC status, not HTTP status

Once you're proxying gRPC, %RESPONSE_CODE% in your access log will always be 200 (the HTTP layer succeeded). What you want is %GRPC_STATUS%, which expands to the protobuf-level outcome: 0 OK, 5 NOT_FOUND, 13 INTERNAL, etc.

Real-world example · Any team running a service mesh on gRPC
Problem
The microservice fleet is gRPC-only — but the marketing site, mobile apps, and a partner integration all want REST/JSON. The team can't ship a hand-coded JSON shim service for every backend.
Envoy solution
One Envoy in front of every gRPC service. Native gRPC clients and other backends talk to Envoy directly. Browsers go through the same Envoy with the grpc_web filter. The next lesson adds the killer feature: Envoy translating JSON ↔ gRPC, so REST clients never even know the backend is gRPC.

What this unlocks

You've turned Envoy into a gRPC-aware reverse proxy. The next two lessons build directly on this:

  • Lesson 18 — JSON ↔ gRPC transcoding so REST clients can hit gRPC.
  • Lesson 19 — auth in front of gRPC (JWT + ext_authz).

By Lesson 21 you'll have a complete production-shaped API gateway that fronts a gRPC backend.

Quick check

Your Envoy proxies HTTP/2 fine, but every gRPC call returns 'upstream connect error or disconnect/reset before headers'. The most likely cause is…

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