Scenario
An e-commerce platform uses Nginx as its API gateway, funneling all microservice requests through this layer. Recently, with increased traffic, the gateway began exhibiting intermittent high latency and 5xx errors, especially when upstream services responded slowly. We were tasked to troubleshoot and tune the Nginx gateway middleware performance.
Symptoms
- Customers report API response times rising from a 200ms average to 1 second or more.
- Nginx error logs show many
upstream timed outandconnection refusedentries. - System load shows high Nginx worker CPU usage, but memory is normal.
- Monitoring charts indicate active connections nearing the
worker_connectionslimit.
Diagnosis
Start by inspecting the Nginx configuration and runtime status.
-
Validate Nginx process and config
bash nginx -t # Validate config syntax nginx -V # Show compile options and modules ps aux | grep nginx -
Analyze logs
bash tail -f /var/log/nginx/access.log | awk '{print $7, $9, $10, $11}' | sort | uniq -c | sort -nr | head -20Check status code distribution to see if 5xx is concentrated. Error logs are typically in/var/log/nginx/error.log. -
Check upstream response performance
bash curl -w 'time_total: %{time_total}s time_connect: %{time_connect}s time_ttfb: %{time_starttransfer}s\n' -o /dev/null http://upstream-service/healthThe difference betweentime_connectandtime_ttfbindicates upstream processing time. If upstream is slow, the issue might not be Nginx. -
Check connection count and worker utilization
bash # View Nginx status page (requires stub_status configuration) curl http://localhost/nginx_status # Or count connections with netstat netstat -an | grep :80 | wc -l -
Packet capture or slow log If you suspect middleware logic (e.g., rewrite, proxy_pass, rate limiting) is causing delays, temporarily enable Nginx
debuglogging or usestraceto trace worker processes.
Commands
After diagnosis, we execute the following targeted actions:
-
Tune worker processes and connection limits: Edit
nginx.conf, setworker_processesto match CPU cores, and increaseworker_connections.nginx worker_processes auto; events { worker_connections 4096; } -
Configure upstream timeouts and retries: In the
locationblock, adjustproxy_connect_timeout,proxy_send_timeout, andproxy_read_timeout. Example:nginx proxy_connect_timeout 5s; proxy_read_timeout 10s; proxy_send_timeout 10s; -
Enable caching or compression: If API responses are cacheable, add
proxy_cache; enablegzipfor textual responses to reduce transfer size. -
Review current configuration:
bash nginx -T # Dump full configurationSave and test config:bash nginx -t && nginx -s reload
Risk Controls
- Backup config before reload:
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak. - Avoid
nginx -s stop: A graceful reload (reload) does not drop connections. - Change one parameter at a time: Use
aborwrkto load-test after each change to catch regressions early. - If concurrency is extremely high, consider adjusting kernel parameters like
net.ipv4.ip_local_port_rangeandnet.core.somaxconn, but this affects the host and should be done carefully.
Rollback
If a change worsens the situation, roll back:
- Restore the backup config:
bash cp /etc/nginx/nginx.conf.bak /etc/nginx/nginx.conf - Validate config:
bash nginx -t - Gracefully reload:
bash nginx -s reload
Verification
- Functional check: Call key APIs and confirm response codes and latencies return to normal.
- Load test: Use
ab -n 10000 -c 500 http://your-gateway/apito observe throughput, latency, and error rates. - Monitor review: Confirm active connections have decreased, 5xx rate is low, and timeout entries are gone from logs.
When to Submit an OpsGlobal Ticket
Consider submitting a ticket to OpsGlobal when:
- The bottleneck cannot be identified at the application layer and may require kernel tuning or infrastructure upgrades.
- Nginx optimization alone cannot meet SLAs, and a more advanced gateway solution (e.g., Kong, Envoy) is needed.
- Multi-team coordination is required for global traffic policy or security audits.
- You need 24/7 monitoring and incident response. OpsGlobal's professional SRE team can assist.
Use cases
Useful for teams handling Performance issues and needing a clear troubleshooting and delivery workflow.
Problem background
A practical deep dive into diagnosing and resolving Nginx API gateway middleware performance issues, covering scenario, symptoms, diagnosis, commands, risk controls, rollback, verification, and when to involve OpsGlobal.
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.