Health checks are an essential part of maintaining a reliable and robust web infrastructure. They ensure that the backend servers are functioning correctly and can handle requests. In this section, we will cover the following topics:
- What are Health Checks?
- Configuring Health Checks in NGINX
- Types of Health Checks
- Practical Examples
- Exercises
- What are Health Checks?
Health checks are mechanisms used to monitor the health and availability of backend servers. They help in:
- Detecting and isolating unhealthy servers.
- Ensuring high availability and reliability.
- Improving load balancing by directing traffic only to healthy servers.
- Configuring Health Checks in NGINX
NGINX supports health checks through the ngx_http_upstream_module
. This module allows you to define health checks for upstream servers.
Basic Configuration
To configure health checks, you need to define an upstream block and specify the health check parameters. Here is a basic example:
http { upstream backend { server backend1.example.com; server backend2.example.com; # Enable health checks health_check; } server { listen 80; location / { proxy_pass http://backend; } } }
Explanation
upstream backend { ... }
: Defines a group of backend servers.server backend1.example.com;
: Specifies a backend server.health_check;
: Enables health checks for the upstream block.
- Types of Health Checks
NGINX supports different types of health checks:
HTTP Health Checks
HTTP health checks send HTTP requests to the backend servers and check the response status code.
http { upstream backend { server backend1.example.com; server backend2.example.com; # Enable HTTP health checks health_check interval=5s fails=3 passes=2 uri=/health_check; } server { listen 80; location / { proxy_pass http://backend; } } }
Explanation
interval=5s
: Specifies the interval between health checks (5 seconds).fails=3
: Marks the server as unhealthy after 3 consecutive failures.passes=2
: Marks the server as healthy after 2 consecutive successes.uri=/health_check
: The URI to be requested for the health check.
TCP Health Checks
TCP health checks establish a TCP connection to the backend servers to verify their availability.
stream { upstream backend { server backend1.example.com:12345; server backend2.example.com:12345; # Enable TCP health checks health_check interval=5s fails=3 passes=2; } server { listen 12345; proxy_pass backend; } }
Explanation
stream { ... }
: Defines a stream block for TCP/UDP traffic.server backend1.example.com:12345;
: Specifies a backend server with a port.health_check interval=5s fails=3 passes=2;
: Configures the health check parameters.
- Practical Examples
Example 1: HTTP Health Check with Custom Headers
http { upstream backend { server backend1.example.com; server backend2.example.com; # Enable HTTP health checks with custom headers health_check interval=5s fails=3 passes=2 uri=/health_check match=custom_headers; } server { listen 80; location / { proxy_pass http://backend; } } # Define custom headers for health checks match custom_headers { headers Host: backend.example.com; headers User-Agent: NGINX-Health-Check; } }
Example 2: TCP Health Check with Custom Timeout
stream { upstream backend { server backend1.example.com:12345; server backend2.example.com:12345; # Enable TCP health checks with custom timeout health_check interval=5s fails=3 passes=2 timeout=2s; } server { listen 12345; proxy_pass backend; } }
- Exercises
Exercise 1: Configure HTTP Health Checks
Task: Configure HTTP health checks for an upstream block with two backend servers. Use the URI /status
for health checks and set the interval to 10 seconds.
Solution:
http { upstream backend { server backend1.example.com; server backend2.example.com; # Enable HTTP health checks health_check interval=10s fails=3 passes=2 uri=/status; } server { listen 80; location / { proxy_pass http://backend; } } }
Exercise 2: Configure TCP Health Checks
Task: Configure TCP health checks for an upstream block with two backend servers on port 8080. Set the interval to 15 seconds and the timeout to 3 seconds.
Solution:
stream { upstream backend { server backend1.example.com:8080; server backend2.example.com:8080; # Enable TCP health checks health_check interval=15s fails=3 passes=2 timeout=3s; } server { listen 8080; proxy_pass backend; } }
Conclusion
In this section, we covered the importance of health checks, how to configure them in NGINX, and the different types of health checks available. We also provided practical examples and exercises to help you understand and implement health checks in your NGINX configuration. Health checks are crucial for maintaining a reliable and high-performing web infrastructure, ensuring that only healthy servers handle client requests.