Home >Operation and Maintenance >Nginx >Nginx virtual host load balancing configuration to achieve multi-domain traffic distribution

Nginx virtual host load balancing configuration to achieve multi-domain traffic distribution

PHPz
PHPzOriginal
2023-07-08 12:49:441869browse

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:

  1. Multiple domain names point to the same Server IP;
  2. Each domain name has been configured in the Nginx configuration file.

2. Nginx virtual host load balancing configuration steps:

  1. 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.

  2. 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.

  3. 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:

  1. 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.
  2. 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. :

  1. 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.

  2. 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.

  3. 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn