0
0
Nginxdevops~10 mins

Health checks in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Health checks
Client sends request
Nginx receives request
Check backend server health
Forward to
Send response to client
Nginx receives a client request, checks if the backend server is healthy, then forwards the request or handles failure accordingly.
Execution Sample
Nginx
upstream backend {
    server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;
    health_check;
}

server {
    location / {
        proxy_pass http://backend;
    }
}
This config sets up a backend with health checks and proxies client requests only to healthy servers.
Process Table
StepActionHealth Check ResultDecisionResponse
1Client sends requestN/ANginx checks backend healthWaiting
2Nginx performs health checkBackend is healthyForward request to backendWaiting for backend response
3Backend responds successfullyHealthy confirmedSend backend response to client200 OK with content
4Next request arrivesBackend is unhealthy (3 fails)Do not forward to backendReturn 502 Bad Gateway
5Health check retries after fail_timeoutBackend recovers and is healthyForward request again200 OK with content
💡 Execution stops after response is sent to client or error returned due to unhealthy backend.
Status Tracker
VariableStartAfter Step 2After Step 4After Step 5
backend_healthunknownhealthyunhealthyhealthy
request_statusnoneforwardedblockedforwarded
response_codenone200502200
Key Moments - 2 Insights
Why does Nginx return 502 Bad Gateway sometimes even though the backend server exists?
Because the health check marked the backend as unhealthy after multiple failures (see execution_table step 4), so Nginx stops forwarding requests to it and returns an error.
How does Nginx decide when to retry sending requests to a previously unhealthy backend?
Nginx uses the fail_timeout setting to wait before retrying health checks (see execution_table step 5), and once the backend is healthy again, it resumes forwarding requests.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the response code sent to the client at step 3?
A200 OK
B502 Bad Gateway
C404 Not Found
D500 Internal Server Error
💡 Hint
Check the 'Response' column in execution_table row for step 3.
At which step does Nginx decide not to forward the request due to backend health?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the step where 'Decision' is 'Do not forward to backend' in execution_table.
If the fail_timeout was increased, how would that affect the execution table?
AStep 3 would return 502 instead of 200
BStep 4 would be skipped
CStep 5 would occur later, delaying backend recovery
DStep 2 would never perform health checks
💡 Hint
Consider when health check retries happen in execution_table step 5.
Concept Snapshot
Nginx Health Checks:
- Use 'health_check;' inside upstream block
- Monitors backend server status
- Stops forwarding to unhealthy servers
- Returns error if no healthy backend
- Retries after fail_timeout period
Full Transcript
This visual execution shows how Nginx handles health checks for backend servers. When a client sends a request, Nginx first checks if the backend is healthy. If healthy, it forwards the request and returns the backend's response. If unhealthy, after multiple failures, Nginx returns an error like 502 Bad Gateway instead of forwarding. After waiting the fail_timeout period, Nginx retries health checks and resumes forwarding if the backend recovers. Variables like backend_health and response_code change accordingly during these steps.