Nginx virtual host load balancing configuration to achieve multi-domain traffic distribution
Introduction:
In modern network applications, load balancing is an important factor in improving system stability and performance. As a high-performance web server, Nginx can use its powerful load balancing function to achieve multi-domain traffic distribution. This article will introduce how to configure Nginx virtual host load balancing to achieve traffic distribution of multiple domain names.
1. Basic environment preparation:
Before we start, we need to ensure that Nginx has been installed and has the following basic elements:
2. Nginx virtual host load balancing configuration steps:
Create a new configuration file vhost.conf:
In the Nginx configuration directory , create a new file named vhost.conf, and add the following content to the file:
http { upstream backend { server backend1.example.com; server backend2.example.com; server backend3.example.com; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; } } }
In this configuration file, we define a load balancing cluster named backend, in which backend1.example. com, backend2.example.com, backend3.example.com are the domain names or IP addresses of the backend servers. In the server block, we specify to listen on port 80 and forward all requests to the backend cluster.
Modify the main configuration file nginx.conf:
Open Nginx’s main configuration file nginx.conf and add the following content to the include statement in the http block:
http { ... include /path/to/vhost.conf; ... }
Replace /path/to/vhost.conf with the actual vhost.conf file path.
Reload Nginx configuration:
Save the modifications to the vhost.conf and nginx.conf files, and run the following command to reload the Nginx configuration file:
nginx -s reload
3. Testing and verification:
After completing the above configuration, we can test and verify through the following steps:
4. Other commonly used configuration options:
In addition to basic load balancing configuration, Nginx also provides many other configuration options to meet different needs. The following are examples of some commonly used configuration options. :
Weight configuration:
upstream backend { server backend1.example.com weight=3; server backend2.example.com weight=2; server backend3.example.com; }
In this example, we set the weight of different back-end servers through the weight parameter. Servers with higher weights will be assigned to higher Lots of traffic.
IP failover:
upstream backend { ip_hash; server backend1.example.com; server backend2.example.com; server backend3.example.com; }
By configuring the ip_hash parameter, Nginx will failover based on the requested IP address, that is, requests for the same IP will always be distributed to The same backend server.
Health check:
http { upstream backend { server backend1.example.com max_fails=2 fail_timeout=30s; server backend2.example.com; server backend3.example.com; } }
By configuring the max_fails and fail_timeout parameters, Nginx can perform health checks on the back-end server. When a server fails max_fails times continuously, it will Temporarily marked as unavailable, the fail_timeout parameter specifies the time interval for the next attempt.
Conclusion:
Nginx virtual host load balancing configuration is an important part of realizing multi-domain traffic distribution. Through reasonable configuration, we can improve the availability and performance of the system. This article provides a basic load balancing configuration example and introduces some common configuration options. Readers can flexibly adjust the configuration according to actual needs to meet their own business needs. I hope this article will be helpful to everyone when configuring Nginx virtual host load balancing.
The above is the detailed content of Nginx virtual host load balancing configuration to achieve multi-domain traffic distribution. For more information, please follow other related articles on the PHP Chinese website!