


Nginx virtual host load balancing configuration to achieve multi-domain traffic distribution
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:
- Multiple domain names point to the same Server IP;
- Each domain name has been configured in the Nginx configuration file.
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; } } }
Copy after loginIn 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; ... }
Copy after loginReplace /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
Copy after login
3. Testing and verification:
After completing the above configuration, we can test and verify through the following steps:
- Modify the local hosts file:
Change example. The resolution address of com is changed to the IP address of the Nginx server. In Windows systems, the hosts file is located in C:WindowsSystem32driversetchosts. In Linux systems, the hosts file is located in /etc/hosts. - Access the test domain name in the browser:
Enter http://example.com in the browser, then refresh the page multiple times to observe whether the request is normally distributed to the back-end server. If everything goes well, you will see multiple backend servers alternately responding to requests.
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; }
Copy after loginIn 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; }
Copy after loginBy 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; } }
Copy after loginBy 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".
