Home >Backend Development >PHP Tutorial >Backend server health check and dynamic adjustment in Nginx load balancing solution
Backend server health check and dynamic adjustment in the Nginx load balancing solution require specific code examples
Abstract: In the Nginx load balancing solution, the backend server’s health check and dynamic adjustment Health status is an important consideration. This article will introduce how to use Nginx's health check module and dynamic adjustment module to implement health check and dynamic adjustment of the back-end server, and give specific code examples.
Health check module
Nginx provides a health check module that can periodically detect the health status of the backend server. This module can be configured in the Nginx reverse proxy server to ensure that only healthy servers receive traffic. The following is a sample code:
http { upstream backend { server backend1.example.com; server backend2.example.com; health_check interval=5s; } server { location / { proxy_pass http://backend; } } }
In the above example, we define an upstream (i.e. backend server cluster) named "backend", which has two servers: backend1.example.com and backend2.example.com. In addition, we also configured a health check module to check the health status of the backend server every 5 seconds.
Dynamic Adjustment Module
Although the health check module can ensure that only healthy servers receive traffic, sometimes we may need to dynamically adjust the load balancing policy while the application is running. Nginx provides a dynamic adjustment module that can automatically adjust the weight of the back-end server according to the load of the server. The following is a sample code:
http { upstream backend { server backend1.example.com weight=1; server backend2.example.com weight=1; dynamic_adjustment; } server { location / { proxy_pass http://backend; } location /adjust { dynamic_adjustment_status; # 输出当前后端服务器的权重信息 } } }
In the above example, we defined an upstream named "backend" and configured two backend servers: backend1.example.com and backend2.example. com. Each server's weight is set to 1. In addition, we also configured a dynamic adjustment module and output the weight information of the current backend server under the "/adjust" path.
Comprehensive example
The following is a comprehensive example that combines the use of the health check module and the dynamic adjustment module:
http { upstream backend { server backend1.example.com; server backend2.example.com; health_check interval=5s; dynamic_adjustment; } server { location / { proxy_pass http://backend; } location /adjust { dynamic_adjustment_status; # 输出当前后端服务器的权重信息 } } }
In this example, we will health The inspection module is used together with the dynamic adjustment module to ensure that only healthy servers receive traffic and dynamically adjust the weight of the backend server based on the load of the server.
Reference link:
The above is the detailed content of Backend server health check and dynamic adjustment in Nginx load balancing solution. For more information, please follow other related articles on the PHP Chinese website!