Sub topic 12
From reverse proxy to API gateway
This chapter assumes you know how a reverse proxy forwards requests to backends, including load balancing and the forwarding headers covered earlier. It shows where a plain reverse proxy ends and an API gateway begins.
What an API gateway adds
An API gateway is a reverse proxy that is the single entry point for an API. It understands which backend service owns which part of the API. It also applies rules that belong to the API as a whole, not to one service.
A reverse proxy mostly answers "which server gets this connection?" A gateway also answers "who is calling, may they call this, and how often?"
Typical gateway duties:
- Routing by path or host.
/users/goes to the user service and/orders/goes to the order service. - Authentication and authorization. The gateway checks an API key or token once, so each service does not repeat the check.
- Rate limiting. The gateway caps requests per client so one caller cannot starve the others.
- Request and response shaping. The gateway rewrites paths, adds headers, or aggregates several backend calls into one response.
- Observability. The gateway logs and measures every call in one place.
A minimal gateway in nginx
nginx can act as a basic gateway. The configuration below routes by path and limits each client IP to 10 requests per second.
# In the http {} context
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=10r/s;
upstream users_service {
server 10.0.1.11:9000;
}
upstream orders_service {
server 10.0.2.11:9000;
}
server {
listen 80;
server_name api.example.com;
limit_req_status 429;
location /users/ {
limit_req zone=api_limit burst=20 nodelay;
proxy_pass http://users_service/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /orders/ {
limit_req zone=api_limit burst=20 nodelay;
proxy_pass http://orders_service/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}Two details matter here:
proxy_pass http://users_service/;ends in a slash. nginx replaces the matched/users/prefix with/, so a request for/users/42reaches the backend as/42. Without the trailing slash, the backend would receive/users/42.burst=20 nodelaylets a client send up to 20 extra requests at once before nginx rejects them with429 Too Many Requests.
Gateway or plain reverse proxy?
| Need | Plain reverse proxy | API gateway |
|---|---|---|
| Spread load across identical servers | Yes | Yes |
| Route different paths to different services | Basic | Yes |
| Per-client keys, quotas, plans | No | Yes |
| Token validation and request transformation | Limited | Yes |
| Developer portal, API versioning | No | Often |
Use a plain reverse proxy when you serve one application. Use a gateway when many services share one public API and you want shared policy in one place.
Common pitfalls
- The gateway becomes a bottleneck. Every request passes through it, so run several instances behind a load balancer.
- Business logic creeps in. Keep the gateway to routing and cross-cutting policy. Logic that changes with each feature belongs in the services.
- Services trust the gateway too much. If a service is reachable directly, callers can skip the gateway's checks. Restrict network access so only the gateway can reach the backends.
- Rate limits use the wrong IP. If another proxy sits in front of the gateway,
$binary_remote_addris that proxy's address, so all clients share one limit. Recover the real client address first.
Exercise
A backend receives requests for /42 when clients call /users/42 through the gateway. Which part of the nginx configuration causes this, and how would you make the backend see /users/42?
Show answer
The trailing slash in proxy_pass http://users_service/; makes nginx replace the matched /users/ prefix with /. To pass the full original path, remove the trailing slash: proxy_pass http://users_service;.