Proxy Inverso

Proxy de borde · Ingress

Escrito por Darjus Kazakevic · revisado el

One front door for many services, with TLS, routing and logs done once.

A reverse proxy is the server that clients actually connect to. It holds the TLS certificate and forwards each request to an internal service, chosen by host name or path. NGINX, Envoy, HAProxy, Caddy and Traefik are common choices. In Kubernetes, an Ingress controller is a reverse proxy.

You set up TLS, compression, size limits and access logs once, not in every service. The cost is one more hop and one more place where a request can change. Headers are added or removed, and two sets of timeouts now apply.

En esencia — Put one server at the edge that terminates TLS, routes by host or path, and hides how many services sit behind it.

Cuándo recurrir a él

Recurre a él cuando

  • Several services need TLS, compression and access logs done one way
  • One public host name must route by path or host to different services
  • Internal services must be hidden behind one entry point

Reconsidéralo cuando

  • One service on a platform whose edge already terminates TLS
  • The front door must log users in or combine calls: use an API gateway
  • Service-to-service calls that a service mesh already handles

Conceptos clave

TLS termination
The proxy decrypts HTTPS and talks plain HTTP, or new TLS, to the services behind it. Certificates live in one place, and automatic renewal, for example with Let's Encrypt, happens once.
Routing rules (Host and path routing)
The proxy matches each request against rules, such as the host api.example.com or the path /assets/, and picks the upstream service. The routing table is most of the configuration.
Forwarded headers
Behind a proxy, the service sees the proxy's IP address and plain HTTP. The proxy adds X-Forwarded-For, X-Forwarded-Proto and Host, or the standard Forwarded header, so the service knows the real client.
Edge limits and buffering
Limits applied before a request reaches any service: maximum body size, header size and slow-client timeouts. The proxy also buffers slow uploads, so a service does not hold a worker for minutes.

Reglas de diseño

Keep business logic out of the proxy
TLS, compression, routing and size limits belong here. Login checks, per-customer quotas and response changes turn it into an API gateway. Decide which box you are building before you add a rule.
Pass on what the service cannot see
Set X-Forwarded-For, X-Forwarded-Proto and Host on every request. Make the service trust them only from the proxy's address, or anyone can fake their IP.
Set the proxy timeout a bit above the service's
The shorter timeout wins. If the proxy stops at 30 s while the service runs for 60 s, users get a 504 while the work goes on. Give the proxy a few seconds more than the service.

Errores habituales

  • The service builds redirect URLs from what it sees: http:// and port 3000. Users are sent to an address that does not work. Pass X-Forwarded-Proto and Host.
  • The service trusts X-Forwarded-For from anyone. A client sends a fake IP and gets around IP limits or hides in the logs.
  • The proxy timeout is shorter than the service's. Users get a 504 while the work later succeeds, so their retry runs it twice.
  • Business rules creep into proxy config. Auth checks and per-customer rules end up in NGINX files that no test covers.