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:
upstream connect error or disconnect/reset before headers
The fix lives in typed_extension_protocol_options on the cluster:
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 }}}
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
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:
timeout: 0son streaming RPCs. Server-streaming and bidi calls live for a long time; a finite request timeout will cut them off mid- stream. Useidle_timeouton the HCM instead if you need protection.grpc_webfilter lets browsers in. More on that below.
Routing by service or method
The path is always /<fully.qualified.Service>/<Method>:
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
Use the public gRPC reflection demo at grpcb.in:
- Save the Envoy config above with a single change — the cluster points
at
grpcb.in:443with TLS:
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 }}}
-
Run Envoy as usual.
-
Install
grpcurland call through:
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.
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.
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.
Your Envoy proxies HTTP/2 fine, but every gRPC call returns 'upstream connect error or disconnect/reset before headers'. The most likely cause is…