Scenario
An e-commerce API gateway (Nginx) experienced P99 latency spikes from 50ms to 3s during a flash sale, with some requests returning 502. Upstream was a Spring Boot app in Kubernetes.
Symptoms
- P99 latency >2s
- Increased upstream connection errors (upstream timed out)
- Nginx error log: "upstream prematurely closed connection"
- High Nginx worker CPU even though traffic below peak
Diagnosis
- Check nginx status:
curl http://localhost/nginx_status– look at Active connections and Waiting. - Stub_status: poor accepts/handled ratio indicates low connection reuse.
- Load test with wrk:
wrk -t4 -c200 -d30s http://gateway/apimeasure latency distribution. - Analyze access log:
awk '{print $NF}' access.log | sort | uniq -c | sort -rnto see response time buckets.
Tuning Commands
1. Enable Upstream Keepalive
Edit upstream block:
upstream backend {
server app-svc:8080;
keepalive 32;
}
Pass keep-alive header in location:
proxy_set_header Connection "";
proxy_http_version 1.1;
2. Buffer Tuning
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 32k;
proxy_busy_buffers_size 64k;
client_body_buffer_size 128K;
3. Timeout Adjustments
proxy_connect_timeout 10s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
4. Worker Connections
event {
worker_connections 4096;
}
Risk Controls
- Backup config:
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak - Validate syntax:
nginx -t - Graceful reload:
nginx -s reload(non-disruptive) - Test in staging first
Rollback
cp /etc/nginx/nginx.conf.bak /etc/nginx/nginx.conf
nginx -s reload
Verification
- Re-run load test: P99 latency should drop below 200ms.
- Check nginx_status: Active connections stable, Waiting queue low.
- Error log: no more timeout entries.
- Monitor alerts cleared.
When to Submit an OpsGlobal Ticket
- Tuning ineffective, errors persist.
- Need deep analysis of Nginx epoll event loop.
- Multi-cloud gateway architecture complexity.
- Custom Nginx module development required.
Use cases
Useful for teams handling Performance issues and needing a clear troubleshooting and delivery workflow.
Problem background
A practical walkthrough of diagnosing and tuning Nginx as an API gateway, covering buffer, keepalive, timeout optimizations with safe rollback procedures.
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.