๐ NGINX API GATEWAY โ END-TO-END TUTORIAL¶
An API Gateway is a single entry point that receives all requests and routes them to various services:
NGINX can perform:
โ Routing
โ Authentication
โ Rate limiting
โ Caching
โ SSL termination
โ Logging & Monitoring
๐งฑ 1. Example Microservices Architecture¶
We assume:
-
Auth Service โ http://127.0.0.1:4001
-
User Service โ http://127.0.0.1:4002
-
Product Service โ http://127.0.0.1:4003
API Gateway runs on port 80/443.
๐ 2. Create the API Gateway Config¶
Create file:
Paste this:
server {
listen 80;
server_name api.example.com;
# -------- AUTH SERVICE --------
location /auth/ {
proxy_pass http://127.0.0.1:4001/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
# -------- USER SERVICE --------
location /users/ {
proxy_pass http://127.0.0.1:4002/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
# -------- PRODUCT SERVICE --------
location /products/ {
proxy_pass http://127.0.0.1:4003/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
๐ 3. Enable the Gateway¶
sudo ln -s /etc/nginx/sites-available/api_gateway /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Now your API Gateway is live:
๐ฅ 4. Add Rate Limiting (API Throttling)¶
Add inside server {}:
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=10r/s;
location / {
limit_req zone=api_limit;
}
This means:
-
Max 10 requests per second per IP
-
Extra requests โ 429 Too Many Requests
๐ 5. Add Basic Authentication (for Internal APIs)¶
Create auth file:
Enable authentication for any route:
location /products/ {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.api_pass;
proxy_pass http://127.0.0.1:4003/;
}
๐ 6. Block Unauthorized Traffic (Whitelist/Blacklist)¶
Block specific IP:¶
Whitelist internal network:¶
๐ 7. Add HTTPS (SSL Termination)¶
Install Certbot:
Run:
Your gateway now supports:
โ HTTPS
โ Automatic redirect
โ Automatic renewal
โก 8. Add Caching for Specific APIs¶
Inside location block:
High-traffic API? Use microcaching:
๐ 9. Load Balancing Behind API Gateway¶
Add in upstream:
upstream users_service {
least_conn;
server 127.0.0.1:4002;
server 127.0.0.1:4004;
}
location /users/ {
proxy_pass http://users_service/;
}
This gives:
-
Load balancing
-
Failover
-
High availability
๐งช 10. Add JWT Authentication (API Security)¶
If you want to validate JWT tokens in API Gateway:
location /users/ {
if ($http_authorization = "") {
return 401;
}
# Forward authorization header
proxy_set_header Authorization $http_authorization;
proxy_pass http://127.0.0.1:4002/;
}
This works with:
-
Auth0
-
Firebase
-
Cognito
-
Your own Auth service
๐ 11. Logging & Monitoring¶
Access logs:
Error logs:
You can integrate this with:
-
Grafana
-
Loki
-
Prometheus
-
ELK Stack
Let me know if you want full setup.
๐ Your API Gateway is Complete!¶
You now have:
โ Routing & Reverse Proxy
โ Multiple microservices
โ JWT Forwarding
โ Rate Limiting
โ Authentication
โ Load Balancing support
โ HTTPS SSL termination
โ Caching (microcaching)
โ IP filtering
โ Logging & Monitoring