Architecture
API Gateway Explained: One Front Door for Your Services
By NileVPS Team · Published · 4 min read
اقرأ المقال بالعربيOnce an application grows past a single backend, clients start to face an awkward question: which server do I call for what? A mobile app may need user data from one service, orders from another and payments from a third. An API gateway answers that question by putting one entry point in front of all of them. Clients talk to the gateway, and the gateway decides where each request goes and what checks it must pass first. This article explains what a gateway does, when it is worth adding, and how to build a simple one on a VPS.
What an API gateway does
At its core, a gateway is a reverse proxy that understands APIs. Typical responsibilities:
- Routing: send
/users/*to the user service and/orders/*to the order service, by path, host, header or version. - TLS termination: handle HTTPS certificates in one place so internal services can stay simple.
- Authentication: validate API keys or JWT tokens before a request reaches any backend.
- Rate limiting and quotas: protect services from abusive or buggy clients.
- Request and response shaping: add headers, strip internal ones, set timeouts, compress responses.
- Observability: one consistent access log and metrics point for all API traffic.
Some gateways also aggregate calls (one client request fans out to several services and returns a combined result), cache responses, or translate between protocols such as REST and gRPC.
Gateway vs reverse proxy vs load balancer
The terms overlap, and the same software can play all three roles.
| Component | Main job | Typical knowledge |
|---|---|---|
| Reverse proxy | Forward requests to a backend, hide its address | HTTP, TLS, caching |
| Load balancer | Spread traffic across identical instances | Health checks, balancing algorithms |
| API gateway | Policy and routing for many different APIs | Auth, keys, quotas, versions, per-route rules |
If you only proxy one app, you have a reverse proxy. When you start adding per-route authentication, per-client rate limits and versioned routes for several services, you are running a gateway, whatever the software is called.
When you need one (and when you don't)
A gateway helps when:
- You have several backend services, or plan to split a monolith (see our guide to microservices).
- External clients, partners or mobile apps consume your API and you need keys and quotas.
- You want auth, logging and limits enforced consistently instead of re-implemented in every service.
It is probably overkill when you have one application behind one domain. The gateway is also an extra hop and a component that can fail, so it must be at least as reliable as the services behind it.
A minimal gateway with Nginx
Nginx is enough for many small and medium setups. This example routes two services, enforces a rate limit and requires an API key header:
limit_req_zone $http_x_api_key zone=perkey:10m rate=10r/s;
map $http_x_api_key $api_client {
default "";
"k_live_abc123" "mobile-app";
"k_live_def456" "partner-a";
}
upstream users_svc { server 127.0.0.1:3001; keepalive 32; }
upstream orders_svc { server 127.0.0.1:3002; keepalive 32; }
server {
listen 443 ssl http2;
server_name api.example.com;
ssl_certificate /etc/letsencrypt/live/api.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/api.example.com/privkey.pem;
if ($api_client = "") { return 401; }
limit_req zone=perkey burst=20 nodelay;
limit_req_status 429;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header X-Client $api_client;
proxy_read_timeout 15s;
location /v1/users/ { proxy_pass http://users_svc/; }
location /v1/orders/ { proxy_pass http://orders_svc/; }
}Test and reload with sudo nginx -t && sudo systemctl reload nginx. Keep real keys out of the main config by putting the map block in a separate file with restricted permissions and loading it with include.
For JWT validation, a developer portal or per-consumer analytics, dedicated open-source gateways such as Kong, Traefik, Tyk, KrakenD or Apache APISIX are worth evaluating. They run fine in Docker on a VPS.
Design checklist
- Keep business logic out of the gateway. It should route and enforce policy, not compute prices.
- Set timeouts on every route. A slow backend should not hold gateway workers forever.
- Return clear status codes: 401 for missing auth, 403 for forbidden, 429 for rate limited, 502/504 for backend failures.
- Pass a request ID (for example
proxy_set_header X-Request-ID $request_id;) so you can trace one call across services. - Version your routes (
/v1/,/v2/) so you can change backends without breaking clients. - Run at least two instances for anything important, behind a load balancer or floating IP.
- Lock down backends: bind them to
127.0.0.1or a private network and allow only the gateway through the firewall.
Hosting the gateway
For a small API, the gateway and services can share one VPS. As traffic grows, move the gateway to its own instance so a heavy service cannot starve it of CPU, and place it in the region closest to your users. You can start a server in a few minutes from /deploy and resize later as load grows.
FAQ
Is an API gateway the same as a load balancer?
No, though they overlap. A load balancer spreads traffic across copies of the same service. A gateway applies routing and policy, such as authentication and rate limits, across many different services. Many gateways include load balancing as one feature.
Does an API gateway slow down requests?
It adds a network hop and some processing, usually small compared with backend work, especially when the gateway runs close to the services and reuses connections with keepalive. Heavy plugins and synchronous auth calls add more, so measure before and after.
Can I run an API gateway on a single VPS?
Yes. Nginx or a containerized gateway runs comfortably on a small VPS alongside a few services. The main limitation is that the server becomes a single point of failure, so add a second instance once uptime matters.