Nginx Reverse Proxy and Load Balancing (Backend Review)

First, Nginx fundamentals

1. Introduction of Nginx

Nginx is an open source high-performance, reliable HTTP middleware, proxy service. It is a Web server that can be used as a reverse proxy, load balancer, and HTTP cache

2. Nginx origin

In the early

In the initial stage of a website, the traffic is relatively small, so the architecture may be that users can access the Tomcat server corresponding to the IP address of the back-end server through domain name and domain name resolution.

The development of

As the user traffic increases, a Tomcat server cannot meet the user’s request, and people come up with two solutions:

  • Vertical scaling: Upgrade server hardware (high cost)
  • Horizontal scaling: Add new servers to share traffic (DNS servers cannot manage so many IP addresses)
    • If a server at the back end breaks down, the DNS server does not know that the server is down, but still resolves this IP address. If a user accesses this IP address, the system cannot provide services for the user.
    • After a new IP address is configured for the DNS server, the NEW IP address does not take effect immediately. Therefore, during the validity period, the new IP address does not provide services for users.

Reverse proxy and load balancer

Now, user access by domain name, DNS server returns a reverse proxy server IP, the reverse proxy server according to the IP configuration of the proxy server and load balancing strategy, to forward the user’s request to different back-end server processing, the backend server will handle the response to the reverse proxy server, through the reverse proxy server to the user. That’s what Nginx does.

The cluster

As the traffic continues to increase, a single reverse proxy server becomes the bottleneck, so we will cluster it to solve the problem of high performance and high availability.

Second, Nginx reverse proxy implementation

1. Forward proxy and reverse proxy

Forward agent

  • Forward agent to hide the real client, the server doesn’t know is which a client request to the server, while the client is the server via a proxy server request, but because only know of a service is the request of the proxy server, so the forward agent can be a real client and proxy server the whole as a client

The reverse proxy

  • A reverse proxy is the opposite of a forward proxy. In a reverse proxy, the client does not know who the server is, but only knows that the proxy server brings the response. Therefore, in a reverse proxy, the server and the proxy server can be regarded as the server.

2. Reverse proxy instance

Start Tomcat8080 port

Forward the local IP plus listening port request to port 8080

server { listen 8082; #server_name localhost; Location / {proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; index index.html index.htm index.jsp; }Copy the code

Third, Nginx load balancing implementation

1. What is load balancing?

Load balancing is when users access an Nginx server through an (IP) access portal, and the Nginx server distributes requests to a back-end Tomcat server through a load balancing policy

2. How does the load balancer select the back-end server to forward

  • The first is to ensure that the selected back-end server is working properly and responding to user requests

  • The second is to select from the pool of healthy servers based on the load balancing algorithm.

3. Load balancing algorithm

To discuss load balancing algorithms in detail, let’s take a look at the parameters in the Upstream module in nginx

upstream test.net{ #ip_hash; Server 127.0.0.1:8082; Server 127.0.0.1:8080; # server 127.0.0.1:8080 down; 8080 # server 127.0.0.1: backup; }Copy the code

The server (i.e. the list of back-end servers) is configured in the upstream module. I have configured two servers under the same IP address (conditions do not allow me and I did not get a virtual machine, please understand).

- down: the server configured with down does not participate in load balancing. - weight: indicates the weight. The heavier the weight is. Backup cannot be used with ip_hash. -ip_hash: uses the IP hash load balancing algorithm. Weight \backup cannot be used with the ip_hash keyword. - least_conn Minimum number of connectionsCopy the code
  • Polling:

Select the first backend server in the health pool for the first request, and then in order until the last, and then cycle. You don’t have to worry about the actual number of connections to the server or the current system load

Normally, this parameter is not configured. By default, the polling algorithm is used

Upstream {test.net server 127.0.0.1:8082; Server 127.0.0.1:8080; }Copy the code
  • Ip_hash:

Select the server to forward based on the hash of the source IP of the request. This ensures that certain users can connect to the same server to some extent. Consider this if your application needs to handle state and requires users to be able to connect to the same server as before. (Clients with the same IP address will be mapped to the same back-end server each time when the list of back-end servers remains unchanged.)

upstream test.net{
     ip_hash;
     server 127.0.0.1:8082;
     server 127.0.0.1:8080;
}
Copy the code
  • Minimum connection priority:

Choose the back-end server with the least number of connections (that is, the least pressure) in preference. This method can be considered in the case of long sessions.

upstream test.net{
     least_conn;
     server 127.0.0.1:8082;
     server 127.0.0.1:8080;
}
Copy the code
  • Random assignment:

    A server is randomly selected from the list of back-end servers for access through the system random algorithm

  • Weighted polling:

    Depending on the weight assigned to the backend server, Nginx allocates requests to high-configuration, low-load machines with a higher weight, so that these machines can handle more requests, and low-configuration, high-load machines with a lower weight and fewer requests. Requests are ordered and weighted to the back end.

    Upstream test.net{server 127.0.0.1:8082 weight=3; Server 127.0.0.1:8080 weight = 1; }Copy the code
  • Weighted random:

    Like the weighted polling method, the weighted random method also assigns different weights according to the configuration of the back-end machine and the load of the system. The difference is that it requests the back-end server randomly based on weights, not sequentially.

Example 4.

Upstream www.kevin.com {server 127.0.0.1:8082 weight=3; Server 127.0.0.1:8080 weight = 1; } server { listen 80; #server_name localhost; location / { proxy_pass http://www.kevin.com; proxy_set_header Host $host; index index.html index.htm index.jsp; }Copy the code

Start a Springboot project, the built-in Tomcat is port 8082, because this is an original project, I directly use nginx test, later if you see an error 500 because the request requires a token, but after seeing this, prove that the port is allocated

Open the 8080 tomacat

Using the native IP and listening port access in the browser, Nginx will poll and distribute requests based on weights

First request:

Second request:

Third request: