Scenario
An e-commerce platform uses Nginx as an API gateway. Users report slow API responses; p99 latency jumped from 200ms to 2s.
Symptoms
- Increased 502/504 errors from upstream
- Nginx error logs: "upstream timed out"
- Active connections near worker_connections limit
Diagnosis
- Check Nginx error log:
tail -f /var/log/nginx/error.log - Log upstream response time: add
$upstream_response_timeto access log format - Monitor connection pool:
curl -s http://localhost/nginx_status | grep Active - Analyze buffer settings: default proxy_buffering can cause head-of-line blocking
Commands
Adjust worker_connections (in /etc/nginx/nginx.conf):
events {
worker_connections 4096;
multi_accept on;
}
Enable and tune buffers (in /etc/nginx/conf.d/api_gateway.conf):
location /api/ {
proxy_pass http://upstream;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 32k;
proxy_busy_buffers_size 64k;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
# Enable upstream keepalive
proxy_http_version 1.1;
proxy_set_header Connection "";
keepalive_requests 1000;
keepalive_timeout 120s;
}
Verify syntax: nginx -t; reload: systemctl reload nginx
Risk Controls
- Test on a canary instance first
- Monitor CPU, memory, and connection counts
- Ensure upstream service can handle increased connection reuse
Rollback
Keep backup of original config as .bak, replace and reload:
cp /etc/nginx/conf.d/api_gateway.conf.bak /etc/nginx/conf.d/api_gateway.conf
nginx -s reload
Verification
- Monitor p99 latency reduction via Prometheus/Grafana
- Check error log for fewer timeouts
- Load test:
ab -n 10000 -c 100 http://gateway/api/test
When to Submit an OpsGlobal Ticket
If latency persists after tuning, or root cause is upstream application slowness, database queries, or DNS issues, submit a ticket for OpsGlobal SRE team deep analysis.
Use cases
Useful for teams handling Performance issues and needing a clear troubleshooting and delivery workflow.
Problem background
A real-world scenario demonstrating how to diagnose and resolve high latency in Nginx-based API gateways, with commands, risk controls, and rollback steps.
Troubleshooting steps
Confirm impact and recent changes, collect logs, configuration and metrics, then apply fixes from low to high risk.
Command examples
Replace sample resource names with real values and store passwords, tokens and keys in environment variables.
Risks
Before production changes, confirm backups, access boundaries, change windows and rollback paths.
Rollback plan
Keep original configuration and release versions; roll back config, images or database changes if metrics degrade.
Deliverables
Root-cause notes, key commands, remediation steps, verification results and follow-up recommendations.
Need help with a similar technical issue?
If your servers, Kubernetes, Docker, CI/CD, databases or monitoring systems have similar issues, submit logs and config files for remote diagnosis.