Configure Nginx load balancing with health checks
✓Works with OpenClaudeYou are a system administrator or DevOps engineer. The user wants to configure Nginx as a load balancer with active health checks across multiple backend servers.
What to check first
- Run
nginx -vto confirm Nginx is installed (version 1.9.0+ required for active health checks) - Verify backend servers are accessible:
curl http://backend1:8080/health && curl http://backend2:8080/health - Check current Nginx configuration location with
nginx -Tand locate the main config file (usually/etc/nginx/nginx.conf)
Steps
- Open
/etc/nginx/nginx.confor create a new upstream config file in/etc/nginx/conf.d/upstream.conf - Define an upstream block with multiple backend servers and assign weights to distribute traffic proportionally
- Add the
health_uriparameter to each upstream server to specify which endpoint Nginx will check - Configure health check intervals with
health_interval(milliseconds) andhealth_timeout(how long to wait for response) - Set
health_typetohttpand specifyhealth_http_versionasHTTP/1.1for proper health check requests - In your server block, reference the upstream with
proxy_pass http://upstream_name;to route traffic - Add
proxy_set_header Host $host;andproxy_set_header X-Real-IP $remote_addr;to preserve client info - Test the configuration with
nginx -tand reload withsystemctl reload nginxornginx -s reload
Code
# /etc/nginx/conf.d/load_balancer.conf
upstream backend_servers {
least_conn;
server backend1.local:8080 weight=5 max_fails=3 fail_timeout=30s;
server backend2.local:8080 weight=3 max_fails=3 fail_timeout=30s;
server backend3.local:8080 weight=2 max_fails=3 fail_timeout=30s;
# Active health checks (Nginx Plus feature, requires commercial license)
# For open-source Nginx, use passive health checks with max_fails/fail_timeout
keepalive 32;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend_servers;
proxy_http_version 1.1;
# Preserve original request headers
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Connection "";
# Timeouts
proxy_connect_timeout 5s;
proxy_send_
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Networking Skills
Other Claude Code skills in the same category — free to download.
HTTP Client
Create configured HTTP client with interceptors
Retry Logic
Implement retry logic with exponential backoff
Circuit Breaker
Implement circuit breaker pattern
Request Queue
Queue and batch HTTP requests
Proxy Setup
Set up reverse proxy configuration
SSL Setup
Configure SSL/TLS certificates
DNS Setup
Configure DNS records
Load Balancer
Set up load balancing configuration
Want a Networking skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.