How do you configure gRPC to work correctly when hosted behind a reverse proxy or load balancer, given gRPC's reliance on HTTP/2?
Short answer
Since gRPC requires HTTP/2 end-to-end (not just to the reverse proxy but ideally through it), the reverse proxy (nginx, YARP, Envoy) must be explicitly configured to support HTTP/2 pass-through rather than downgrading to HTTP/1.1, and load balancers must support connection-level…
Advertisement
AModel answer
Since gRPC requires HTTP/2 end-to-end (not just to the reverse proxy but ideally through it), the reverse proxy (nginx, YARP, Envoy) must be explicitly configured to support HTTP/2 pass-through rather than downgrading to HTTP/1.1, and load balancers must support connection-level (not just request-level) awareness since a single HTTP/2 connection can multiplex many concurrent gRPC calls -- naive round-robin load balancing at the TCP connection level can cause uneven load distribution since all calls on one long-lived HTTP/2 connection go to the same backend instance.
# nginx.conf -- gRPC requires HTTP/2 pass-through
server {
listen 443 ssl http2;
location / {
grpc_pass grpcs://backend_service;
}
}
// Kestrel must also be configured for HTTP/2
builder.WebHost.ConfigureKestrel(options => {
options.ListenAnyIP(5001, listenOptions => listenOptions.Protocols = HttpProtocols.Http2);
});Real-world example
A team deploying gRPC services behind an AWS Application Load Balancer discovers uneven load distribution across backend instances, tracing the issue to the ALB's connection-level (not request-level) load balancing combined with long-lived HTTP/2 connections concentrating traffic on whichever instance received the initial connection.
Likely follow-ups
- What's the specific configuration needed for gRPC to work correctly through YARP as a reverse proxy?
- How does client-side load balancing (as an alternative to a proxy-based approach) address the connection-concentration problem?
Related topics