Home  >  Article  >  Backend Development  >  Nginx reverse proxy and load balancing

Nginx reverse proxy and load balancing

WBOY
WBOYOriginal
2016-07-29 09:11:241141browse

What is reverse proxy and load balancing - What is reverse proxy

Sometimes we use our own computer A to access a foreign website B, but cannot access it. At this time, there is an intermediate server C that can access it. Foreign website B, then we can use our own computer to access server C, and use C to access website B. Then at this time, server C is called a proxy server, and this access method is called a forward proxy. One characteristic of forward proxy is that we know exactly which website we want to visit. For another example, when we have a server cluster and the content of each server in the server cluster is the same, we also cannot access the server in the server cluster directly from a personal computer, and at this time the third-party server can access Cluster, at this time, we access the content of the server cluster through a third-party server, but at this time we do not know which server provides the content. The proxy method at this time is called reverse proxy.

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? That's right, we can build many, many servers, which form 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 introduce the access request to the selected server. In this way, every user access will ensure that the pressure of each server in the server cluster tends to be balanced, sharing the server pressure and avoiding server crash.

Implementation of Nginx load balancing

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 will Select a less stressed server from the server cluster table and direct 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.

HTTP Upstream module

Upstream module is an important module of Nginx server. The Upstream module implements backend load balancing between polling and client IPs. Commonly used instructions include the ip_hash instruction, server instruction and upstream instruction. Let’s talk about them separately below.

In a load balancing system, if a user logs in on a certain server, then if the user makes a second request, because we are a load balancing system, each request will be redirected to a server in the server cluster. , then it is obviously inappropriate to relocate users who have logged in to server A to other servers at this time. Therefore, we can use the ip_hash instruction to solve this problem. If the client request has already visited server A and logged in, then the second request will automatically locate the request to the back-end server through the hash algorithm. Below we explain through examples. The

server directive is mainly used to specify the name and parameters of the server. Below we explain through examples.

The upstream command is mainly used to set a group of proxy servers that can be used in the proxy_pass and fastcgi_pass commands. The default load balancing method is polling.

ip_hash command

The above introduces the reverse proxy and load balancing of Nginx, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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