We usually use Nginx for reverse proxy and load balancing, both over HTTP, but Nginx can also forward TCP, and the configuration is basically similar (the following principles apply to HTTP as well).

The schematic diagram is as follows:

Forward and reverse proxies

Forward proxy: this is what we call A proxy. For example, the client sends A request to server A, but A does not accept any request other than server B. Then the client entrusts server B to send the request to server A, and server B acts as A middleman to transfer the information. This process is transparent to server A, which only knows to communicate with server B.

Reverse proxy: The forward proxy is the client delegate, and the reverse proxy is the server delegate. For example, A customer makes A request to server A, but server A does not know how to process the request. Server A entrusts server B to process the request and then returns the result to the customer. This process is transparent to the customer.

All load balancing uses reverse proxies to distribute customer requests to different hosts for processing without the customer knowing about the process.

Load Balancing Policy

Nginx provides round robin, CLIENT IP hash, and weighted polling. By default, Nginx uses polling.

Polling (default)

Each request is allocated to a different backend server one by one in chronological order. If the backend server goes down, the request is automatically removed.

Weighted polling

Specifies the polling probability, weight proportional to the access ratio, for cases where back-end server performance is uneven.

ip_hash

Each request is allocated according to the hash result of the access IP, so that each visitor has a fixed access to the back-end server, which can solve the session problem.

Retry strategy

You can specify the maximum number of retries and retry interval for each Backserver, using the keywords max_fails and fail_timeout.

Engine strategy

When all non-backup servers are down or busy, the standby server marked by Backup is used. It is important to note that backup cannot be used with the ip_hash keyword.

In the nginx.conf file configuration, is the same as HTTP

stream {
    upstream backend {
        #hash $remote_addr consistent;Server 10.118.12.92:10001 weight = 5;# server 127.0.0.1:12345 max_fails = 3 fail_timeout = 30 s;
        #server unix:/tmp/backend3;} server { listen 10001; proxy_connect_timeout 5s; proxy_timeout 3600s; proxy_pass backend; }}Copy the code

Note that if placed in HTTP, this configuration is load-balancing HTTP.