Dump the code

Health checks and failover mechanisms

Created 9 months ago
Posted By admin
5min read
Nginx provides health checks and failover mechanisms to improve the reliability and availability of backend servers in a load-balanced configuration. These features help ensure that Nginx directs traffic only to healthy servers and can automatically detect and handle failures. 

Health Checks:

Health checks are regular check-ups for computer systems. They help us make sure that our servers and services are doing well and responding to requests. Imagine it as a system doctor that regularly looks at things like responsiveness and errors. In load balancing, health checks ensure that only healthy servers get to do the job, automatically identifying and isolating servers that aren't feeling well. It's a way to keep everything running smoothly and make sure users get what they need without any hiccups.

- Example :

Assume you have a backend server running on backend_server1 and you want Nginx to perform a health check by requesting a specific endpoint, such as /health, on that server.

upstream backend {
    server backend_server1_ip:backend_port;
    server backend_server2_ip:backend_port;
    # Add more backend servers as needed
}

server {
    listen 80;
    server_name your_domain.com;

    location / {
        proxy_pass http://backend;
        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;
    }

    # Health check endpoint
    location /health.html {
        proxy_pass http://backend;
        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;

        # Define conditions for a successful health check response
        proxy_intercept_errors on;
        error_page 404 =200 /health_ok.html;
    }

    location = /health_ok.html {
        return 200 "Healthy";
    }
}

  • proxy_intercept_errors on; : This directive enables interception of errors returned by the proxied server. When set to on, Nginx will intercept responses with 4xx and 5xx status codes from the backend server.
  • error_page 404 =200 /health_ok.html;: This line instructs Nginx to intercept a 404 error from the proxied server and respond to the client with a 200 status code. Essentially, it's saying, "If the backend returns a 404 error, treat it as a success (200) and redirect internally to /health_ok.html"
  • /health_ok.html is the internal location to which Nginx redirects the request when a 404 error is intercepted.
  • location = /health_ok.html { return 200 "Healthy"; }: This block handles requests to /health_ok.html and responds with a 200 status code and the text "Healthy." It serves as a marker for a successful health check.

Putting it all together, when Nginx sends a request to the /health.html endpoint and the backend server returns a 404 error (indicating that /health.html is not found), Nginx intercepts that error, transforms it into a 200 status code, and internally redirects the request to /health_ok.html, signifying a successful health check.

Failover mechanisms:

Failover act like having a backup plan for our computer systems. If something goes wrong with our main system, failover kicks in and smoothly switches to a backup system, making sure everything keeps running. Failover mechanisms are our system's way of handling unexpected issues, reducing downtime, and ensuring that our services stay up and running, even when there are bumps in the road.

- Example :

The backup directive in the server block allows you to designate servers as backups. Requests are directed to the backup servers only if all non-backup servers are unavailable.

   upstream backend {
       server backend_server1_ip:backend_port;
       server backend_server2_ip:backend_port backup;
       # Add more backend servers as needed
   }

The max_fails directive sets the number of consecutive failed health checks needed to mark a server as unavailable. The fail_timeout directive specifies the time to consider a server unavailable after reaching the max_fails threshold.

   upstream backend {
       server backend_server1_ip:backend_port max_fails=3 fail_timeout=10s;
       server backend_server2_ip:backend_port max_fails=3 fail_timeout=10s;
       # Add more backend servers as needed
   }

proxy_next_upstream directive allows you to define conditions under which Nginx should try the next server in the upstream group.

   location / {
       proxy_pass http://backend;
       proxy_next_upstream error timeout http_500;
   }

ip_hash directive provides session persistence, ensuring that requests from the same client always go to the same backend server. This can be useful in scenarios where sessions need to be maintained.

   upstream backend {
       ip_hash;
       server backend_server1_ip:backend_port;
       server backend_server2_ip:backend_port;
       # Add more backend servers as needed
   }

Important notes:

  • When using health checks, ensure that the health check endpoint does not put additional load on the server, and it provides accurate information about the server's health.
  • Regularly test failover scenarios to validate that Nginx behaves as expected during server failures.
  • Regularly review Nginx logs for error messages related to upstream servers. Implement monitoring solutions to track the health and performance of your Nginx instances and backend servers.

Remember that the specific implementation details may vary based on the version of Nginx and whether you are using Nginx Open Source or Nginx Plus, as certain features are exclusive to Nginx Plus. Always refer to the official Nginx documentation for the version you are using for the most accurate and up-to-date information.
Topics

Mastering Nginx

27 articles

Bash script

2 articles

Crontab

2 articles