Home>Article>Operation and Maintenance> How to do load balancing in nginx

How to do load balancing in nginx

藏色散人
藏色散人 Original
2019-06-10 10:02:27 4188browse

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://m.sbmmt.com/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