Bird
Raised Fist0
Nginxdevops~10 mins

Health checks in Nginx - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a health check in an nginx setup?
easy
A. To increase the server's CPU speed
B. To verify if a server is ready to handle requests
C. To change the server's IP address automatically
D. To backup server files regularly

Solution

  1. Step 1: Understand health check concept

    Health checks are used to confirm if a server is ready and able to serve requests.
  2. Step 2: Identify the purpose in nginx context

    In nginx, health checks help route traffic only to servers that respond correctly, ensuring reliability.
  3. Final Answer:

    To verify if a server is ready to handle requests -> Option B
  4. Quick Check:

    Health check purpose = Verify readiness [OK]
Hint: Health checks confirm server readiness to serve requests [OK]
Common Mistakes:
  • Thinking health checks speed up the server
  • Confusing health checks with backups
  • Assuming health checks change server IP
2. Which of the following is the correct way to define a simple health check endpoint in nginx configuration?
easy
A. location /healthcheck { return 200 "OK"; }
B. location /healthcheck { proxy_pass http://backend; }
C. location /healthcheck { error_page 404 /404.html; }
D. location /healthcheck { deny all; }

Solution

  1. Step 1: Review nginx location syntax for health check

    A simple health check returns a 200 status with a message like 'OK'.
  2. Step 2: Identify correct return directive

    location /healthcheck { return 200 "OK"; } uses return 200 "OK"; which correctly sends a success response for health checks.
  3. Final Answer:

    location /healthcheck { return 200 "OK"; } -> Option A
  4. Quick Check:

    Simple health check = return 200 [OK]
Hint: Health check returns 200 with OK message [OK]
Common Mistakes:
  • Using proxy_pass instead of return for simple checks
  • Setting error_page instead of success response
  • Denying access blocks health check
3. Given this nginx config snippet, what will be the HTTP status code when accessing /health?
location /health {
  return 204;
}
medium
A. 204
B. 404
C. 500
D. 200

Solution

  1. Step 1: Understand the return directive in nginx

    The return 204; sends an HTTP status code 204 (No Content) response.
  2. Step 2: Match the status code with the request

    Accessing /health triggers this location and returns 204 as configured.
  3. Final Answer:

    204 -> Option A
  4. Quick Check:

    return 204 sends status 204 [OK]
Hint: return 204 sends HTTP 204 No Content status [OK]
Common Mistakes:
  • Assuming default 200 status without checking return
  • Confusing 204 with 404 or 500 errors
  • Ignoring the explicit return directive
4. You configured this health check in nginx:
location /healthcheck {
  return 200 "OK"
}
But the health check fails with a syntax error. What is the problem?
medium
A. The return directive cannot include a message string
B. The return code 200 is invalid
C. The location block is missing a semicolon after return
D. The location path must be /health, not /healthcheck

Solution

  1. Step 1: Check nginx configuration syntax

    All nginx directives must end with a semicolon (;).
  2. Step 2: Locate the error in the snippet

    The return 200 "OK" line is missing the trailing semicolon, causing a syntax error.
  3. Final Answer:

    The location block is missing a semicolon after return -> Option C
  4. Quick Check:

    nginx directives require semicolon [OK]
Hint: Every nginx directive must end with a semicolon [OK]
Common Mistakes:
  • Adding message string directly after return
  • Missing semicolon causing syntax error
  • Using wrong location path for health check
5. You want to configure nginx to perform active health checks on backend servers and mark them unhealthy if they fail. Which directive and module combination should you use?
hard
A. Use proxy_pass with proxy_next_upstream and enable ngx_http_healthcheck_module
B. Use deny all in location to block unhealthy servers
C. Use return 200 in location block for backend health
D. Use check directive inside upstream block with ngx_http_upstream_check_module

Solution

  1. Step 1: Identify active health check method in nginx

    Active health checks require special modules like ngx_http_upstream_check_module and directives like check inside upstream blocks.
  2. Step 2: Match correct directive and module

    Use check directive inside upstream block with ngx_http_upstream_check_module correctly pairs check directive with ngx_http_upstream_check_module for active health checks.
  3. Final Answer:

    Use check directive inside upstream block with ngx_http_upstream_check_module -> Option D
  4. Quick Check:

    Active health checks = check + upstream_check_module [OK]
Hint: Active health checks need check directive and module [OK]
Common Mistakes:
  • Confusing passive proxy_next_upstream with active health checks
  • Using return 200 for backend health checks
  • Blocking servers instead of checking health
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard