Reverse Proxy vs API Gateway
On this page
What a reverse proxy is
Clients shouldn't need to know that /users is served by one service and /orders by another, or that there are twelve servers behind them. A reverse proxy accepts requests on behalf of your servers and forwards them. (A forward proxy is the opposite: it acts for clients, like a corporate web proxy.)
What a reverse proxy does
- TLS termination: certificates live in one place; backends speak plain HTTP on a private network, or re-encrypted TLS.
- Routing by host and path to different services.
- Load balancing across instances (Day 22).
- Caching and compression of responses.
- Buffering: the proxy absorbs slow clients so app servers aren't tied up sending bytes to a phone on a weak connection.
- Hiding the backend: only the proxy is exposed.
nginx, HAProxy, Envoy, Caddy and Traefik are the common choices.
What an API gateway adds
An API gateway is a reverse proxy focused on APIs, adding:
- Authentication: validating API keys, JWTs or OAuth tokens before requests reach services.
- Rate limiting and quotas per client or key.
- Request and response transformation, version routing (
/v1,/v2). - Analytics, developer portals and key management.
Examples: AWS API Gateway, Kong, Apigee, Azure API Management, or Envoy-based gateways.
An nginx API gateway with rate limiting
Path routing plus per-client rate limiting. nginx -t validates it:
events {}
http {
limit_req_zone $binary_remote_addr zone=per_ip:10m rate=5r/s;
limit_req_status 429;
upstream users_service { server 127.0.0.1:9101; }
upstream orders_service { server 127.0.0.1:9102; }
server {
listen 127.0.0.1:9090;
server_name api.example.com;
location /users/ {
limit_req zone=per_ip burst=10 nodelay;
proxy_pass http://users_service/; # trailing slash strips /users/
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /orders/ {
limit_req zone=per_ip burst=10 nodelay;
proxy_pass http://orders_service/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}With two small backends running, then 14 rapid requests:
$ curl -s http://127.0.0.1:9090/users/42
users handled /42
$ curl -s http://127.0.0.1:9090/orders/981
orders handled /981
200 200 200 200 200 200 200 200 200 200 200 429 429 429Two details worth checking in your own configs:
proxy_pass http://users_service/;with a trailing slash replaces the matched/users/prefix, so the service sees/42. Without the slash, it would see/users/42. Getting this wrong is a classic source of 404s.- The limit allows a burst of 10 on top of the current request, then answers
429. Withoutlimit_req_status 429, nginx returns 503, which clients and monitoring misread as an outage.
In production, listen 443 ssl with certificates replaces the local listener.
Reverse proxy, gateway, or mesh?
| Need | Use |
|---|---|
| TLS, routing and caching for a website or a few services | A reverse proxy |
| Public APIs with keys, quotas and per-client auth | An API gateway |
| Service-to-service traffic inside the cluster (mutual TLS, retries, telemetry) | A service mesh (Day 25) |
Keep business logic out of the gateway. Gateways that accumulate custom transformations and orchestration become a bottleneck every team has to change.
Check yourself
A forward proxy acts for clients, sending their requests out to many servers, such as a corporate web proxy. A reverse proxy acts for servers, receiving requests from many clients and forwarding them to backends, such as nginx in front of an app.
The proxy_pass URL lacks a trailing slash or path, so nginx forwards the original path unchanged. With location /users/ { proxy_pass http://app/; }, the trailing slash makes nginx replace the matched /users/ prefix.
When you expose APIs to external clients and need per-client authentication, API keys, quotas, usage analytics and a developer portal. For internal routing and TLS termination, a reverse proxy is usually enough.
limit_req_zone tracks request rates per key, such as client IP, using a leaky bucket. limit_req applies it to a location, with burst allowing short spikes.
When the concern is traffic between many internal services: mutual TLS, retries, timeouts and observability for every service-to-service call. A gateway handles traffic entering from outside; a mesh handles the east-west traffic inside.