NGINX - load balancing

Load balancing ———> through reverse proxy to achieve

Seven-layer proxy and four-layer proxy of nginx reverse proxy

Seven layers of proxy:

The most commonly used reverse proxy method for seven-layer proxy can only be configured in the http module of the nginx configuration file, and the method name must be defined as the "upstream" module. Note that it cannot be written in the server module or in the In the location module;

At the same time, the upstream module is an independent module in the http module

Seven-layer proxy: the proxy is http request and response

The working principle of the seven-layer proxy:

Client --> http request --> seven-layer proxy (on the proxy server) --> the proxy server forwards http, requesting to a set of internal servers (web cluster) --> the client does not know whether the server is still in the era of the request Internal server, and the ip of the internal server is hidden through the proxy server.

        In fact, the proxy server is accessed, the request is sent to the proxy, and the proxy forwards it to the web server, and the web server responds———————actually the web server responds

Layer 4 Proxy

The four-layer proxy is based on the tcp/ip protocol, and the proxy forwarding method of the protocol layer can realize load-balanced forwarding based on the ip address and port number; the four-layer proxy cannot obtain the URL information in the http request, but can only process the tcp/udp data packet For forwarding, traffic forwarding, the name of the configuration method: stream; stream cannot be configured in the http module, it is configured in the global module and belongs to an independent module

The difference between a four-tier proxy and a seven-tier proxy (emphasis!!)

1. The seven-layer proxy uses http requests; while the four-layer proxy uses tcp/udp packets and forwards traffic;

        Seven-layer proxy: http request, which can analyze and process the request in depth. Such as: flow control, content filtering;

        Four-layer proxy: no flow control, and no content filtering;

        

        Four-layer proxy is usually suitable for: scenarios that need to handle a large number of connection requests

        Seven-layer proxy is usually suitable for: the scene of precise processing and control of requests

                In actual work, the four-tier proxy and the seven-tier proxy can be used together

2. The speed of the four-tier agent is faster than that of the seven-tier agent;

        reason:

        (1): The four-layer proxy is for traffic forwarding, and cannot analyze and control the request, so it must be fast

        (2): The four-layer agent uses the kernel, and the kernel forwards the traffic, which is fast

      Layer 7 proxy is relatively slow

        reason:

        (1): The seven-layer agent processes and parses the request, which is slow

        (2): The seven-layer proxy is in user mode, access control, and traffic processing; so it is slow

                Seven-layer proxy can provide higher service and higher user experience

Forward proxy and reverse proxy

Forward proxy:

The module proxy_pass configures the access address of the proxy server, this module can only be written in the location module

reverse proxy:

The client accesses the proxy server, and the proxy server forwards requests or traffic to the back-end server. There will be multiple back-end web servers, but the user does not know which server is finally accessed

        The role of reverse proxy: load balancing, high availability, scalability, and improved maintainability.

How to do reverse proxy at work?

Reverse proxy --> load balancing

        upstream: load balancing based on http, reverse proxy

Features:

1. The load balancing method of http requests

2. No cache

3. Load balancing algorithm:

(1) Default algorithm: polling (rr); requests are assigned to the backend server in turn. The polling algorithm is used when multiple web servers have similar processing capabilities. The default algorithm can be omitted.

(2) Weighting algorithm: Based on the polling algorithm, by weighting different web servers, servers with stronger processing capabilities can allocate more requests; (Note: Although the weight is configured, the result of the fall may not always be accurate)

(3) ip_hash: Calculate a hash value based on the IP address. After using the ip_hash algorithm, the request of the same client will be ibei allocated to the same backend server to ensure the stability of the session; when the number of backend servers changes, The hash value will be recalculated, and the requested server may also change

(4) The minimum number of connections: least_conn; will poll, features: will send the request to the backend web server with the least number of current connections, which is suitable for situations where the backend server takes different time to process tasks and avoids all requests being concentrated in On back-end servers with stronger processing capabilities, the least_conn algorithm will be used in combination with the weighted round-robin algorithm

(5) url_hash: Calculate the hash value based on the URI address. When using the url_hash algorithm, the URI of the same request will be assigned to the same backend server

        Usage scenarios of load balancing algorithms:

        Small-scale scenarios: the amount of concurrency is small, and the default algorithm can meet the applicable conditions

        The processing capability of the back-end web server can be used in conjunction with the difference, weighted round-robin and the minimum number of connections

        Large-scale concurrency: ip_hash, url_hash; after the first request, a local cache will be generated, and because of the hash algorithm, the requested back-end web server will not change, which can improve the access speed, and the local cache is accessed. Reduce the request capacity of the backend server

        important point:

ip_hash: When the number of backend servers changes, the requested server may change

uri_hash: When the requested address changes, the requested server may also change

Guess you like

Origin blog.csdn.net/ZZZ_CCC01/article/details/132187092