search
HomeOperation and MaintenanceNginxNginx virtual host load balancing configuration to achieve multi-domain traffic distribution

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

Jul 08, 2023 pm 12:49 PM
nginxload balancingMultiple domain names

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
NGINX and Apache: Understanding the Key DifferencesNGINX and Apache: Understanding the Key DifferencesApr 26, 2025 am 12:01 AM

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

NGINX Unit: Key Features and CapabilitiesNGINX Unit: Key Features and CapabilitiesApr 25, 2025 am 12:17 AM

NGINXUnit is an open source application server that supports multiple programming languages ​​and provides functions such as dynamic configuration, zero downtime updates and built-in load balancing. 1. Dynamic configuration: You can modify the configuration without restarting. 2. Multilingual support: compatible with Python, Go, Java, PHP, etc. 3. Zero downtime update: Supports application updates that do not interrupt services. 4. Built-in load balancing: Requests can be distributed to multiple application instances.

NGINX Unit vs. Other Application ServersNGINX Unit vs. Other Application ServersApr 24, 2025 am 12:14 AM

NGINXUnit is better than ApacheTomcat, Gunicorn and Node.js built-in HTTP servers, suitable for multilingual projects and dynamic configuration requirements. 1) Supports multiple programming languages, 2) Provides dynamic configuration reloading, 3) Built-in load balancing function, suitable for projects that require high scalability and reliability.

NGINX Unit: The Architecture and How It WorksNGINX Unit: The Architecture and How It WorksApr 23, 2025 am 12:18 AM

NGINXUnit improves application performance and manageability with its modular architecture and dynamic reconfiguration capabilities. 1) Modular design includes master processes, routers and application processes, supporting efficient management and expansion. 2) Dynamic reconfiguration allows seamless update of configuration at runtime, suitable for CI/CD environments. 3) Multilingual support is implemented through dynamic loading of language runtime, improving development flexibility. 4) High performance is achieved through event-driven models and asynchronous I/O, and remains efficient even under high concurrency. 5) Security is improved by isolating application processes and reducing the mutual influence between applications.

Using NGINX Unit: Deploying and Managing ApplicationsUsing NGINX Unit: Deploying and Managing ApplicationsApr 22, 2025 am 12:06 AM

NGINXUnit can be used to deploy and manage applications in multiple languages. 1) Install NGINXUnit. 2) Configure it to run different types of applications such as Python and PHP. 3) Use its dynamic configuration function for application management. Through these steps, you can efficiently deploy and manage applications and improve project efficiency.

NGINX vs. Apache: A Comparative Analysis of Web ServersNGINX vs. Apache: A Comparative Analysis of Web ServersApr 21, 2025 am 12:08 AM

NGINX is more suitable for handling high concurrent connections, while Apache is more suitable for scenarios where complex configurations and module extensions are required. 1.NGINX is known for its high performance and low resource consumption, and is suitable for high concurrency. 2.Apache is known for its stability and rich module extensions, which are suitable for complex configuration needs.

NGINX Unit's Advantages: Flexibility and PerformanceNGINX Unit's Advantages: Flexibility and PerformanceApr 20, 2025 am 12:07 AM

NGINXUnit improves application flexibility and performance with its dynamic configuration and high-performance architecture. 1. Dynamic configuration allows the application configuration to be adjusted without restarting the server. 2. High performance is reflected in event-driven and non-blocking architectures and multi-process models, and can efficiently handle concurrent connections and utilize multi-core CPUs.

NGINX vs. Apache: Performance, Scalability, and EfficiencyNGINX vs. Apache: Performance, Scalability, and EfficiencyApr 19, 2025 am 12:05 AM

NGINX and Apache are both powerful web servers, each with unique advantages and disadvantages in terms of performance, scalability and efficiency. 1) NGINX performs well when handling static content and reverse proxying, suitable for high concurrency scenarios. 2) Apache performs better when processing dynamic content and is suitable for projects that require rich module support. The selection of a server should be decided based on project requirements and scenarios.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor