search
HomeOperation and MaintenanceNginxHow to do load balancing in nginx

How to do load balancing in nginx

Jun 10, 2019 am 10:02 AM
nginx load balancing

How to do load balancing in nginx

1. Load balancing

When the number of visits per unit time of a server increases, the pressure on the server will increase. . When the pressure on a server exceeds its capacity, the server will crash. In order to avoid server crashes and provide users with a better experience, we usually use load balancing to share the pressure on the server.

So what is load balancing? We build many servers and form these servers into a server cluster. Then, when a user visits our website, he first accesses an intermediate server, and then lets the intermediate server choose a server with less pressure in the server cluster, and then The access request is directed to the selected server.

In this way, every time a user visits, it will ensure that the pressure of each server in the server cluster tends to be balanced, sharing the server pressure and avoiding server crash. Load balancing uses the idea of ​​reverse proxy.

2. Implementation of load balancing in Nginx

Nginx is a server that can achieve load balancing through reverse proxy. When using the Nginx service to achieve load balancing, The user's access will first access the Nginx server, and then the Nginx server selects a server with less pressure from the server cluster table, and then directs the access request to that server. If a server in the server cluster crashes, the server will be deleted from the list of candidate servers. That is to say, if a server crashes, Nginx will definitely not introduce access requests to the server.

1. Create the fzjh.conf configuration file

Create the fzjh.conf file under /usr/local/nginx/conf. The file content is as follows:

user  nobody;
worker_processes  2;
events {
    worker_connections  1024;
}
http{
   #=upstream设置负载均衡的待选服务器列表,在运行中如果有服务器崩溃那该服务器就会在该列表中移除
    upstream mypro{
        server 219.133.55.36;#中国万维网
        server 115.239.210.27;#百度的ip
    }
    server{
        listen 8080;
        #====== 对ngnix更目录做负载均衡===
        location / {
           #==选择要代理的服务器要与upstream对应==========
            proxy_pass http://mypro;
        }
    }
}

2. Load the fzjh.conf configuration file

Execute command:

 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/fzjh.conf

3. Whether the test is successful

Here, our load balancing candidate server list is Baidu and China World Wide Web, which means that we can test it by accessing my host name 8080. If the page switches between Baidu and China World Wide Web, it means success.

This article comes from the Nginx tutorial column: http://www.php.cn/nginx/

The above is the detailed content of How to do load balancing in nginx. 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 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.

The Ultimate Showdown: NGINX vs. ApacheThe Ultimate Showdown: NGINX vs. ApacheApr 18, 2025 am 12:02 AM

NGINX is suitable for handling high concurrent requests, while Apache is suitable for scenarios where complex configurations and functional extensions are required. 1.NGINX adopts an event-driven, non-blocking architecture, and is suitable for high concurrency environments. 2. Apache adopts process or thread model to provide a rich module ecosystem that is suitable for complex configuration needs.

NGINX in Action: Examples and Real-World ApplicationsNGINX in Action: Examples and Real-World ApplicationsApr 17, 2025 am 12:18 AM

NGINX can be used to improve website performance, security, and scalability. 1) As a reverse proxy and load balancer, NGINX can optimize back-end services and share traffic. 2) Through event-driven and asynchronous architecture, NGINX efficiently handles high concurrent connections. 3) Configuration files allow flexible definition of rules, such as static file service and load balancing. 4) Optimization suggestions include enabling Gzip compression, using cache and tuning the worker process.

NGINX Unit: Supporting Different Programming LanguagesNGINX Unit: Supporting Different Programming LanguagesApr 16, 2025 am 12:15 AM

NGINXUnit supports multiple programming languages ​​and is implemented through modular design. 1. Loading language module: Load the corresponding module according to the configuration file. 2. Application startup: Execute application code when the calling language runs. 3. Request processing: forward the request to the application instance. 4. Response return: Return the processed response to the client.

Choosing Between NGINX and Apache: The Right Fit for Your NeedsChoosing Between NGINX and Apache: The Right Fit for Your NeedsApr 15, 2025 am 12:04 AM

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.

How to start nginxHow to start nginxApr 14, 2025 pm 01:06 PM

Question: How to start Nginx? Answer: Install Nginx Startup Nginx Verification Nginx Is Nginx Started Explore other startup options Automatically start Nginx

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

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software