What concepts should be mastered when learning Nginx for beginners

Why use Nginx?

When the small company's project was just launched, the amount of concurrency was small and users used it less, so in the case of low concurrency, a jar package is enough to start the application, and then the internal tomcat returns the content to the user.

But slowly, more and more users are using the platform, and the amount of concurrency is gradually increasing. At this time, one server cannot meet our needs.

So we scaled out and added servers. At this time, several projects are started on different servers. If users want to access, they need to add a proxy server, which will help us forward and process requests through the proxy server.

What is Nginx?

Nginx (engine x) is a high-performance HTTP and reverse proxy web server, and also provides IMAP/POP3/SMTP services. Nginx was developed by Igor Sysoyev for the second most visited Rambler.ru site in Russia (Russian: Рамблер). The first public version 0.1.0 was released on October 4, 2004. On June 1, 2011, nginx 1.0.4 was released.

It is characterized by less memory and strong concurrency. In fact, the concurrency of nginx is better than other web servers of the same type. Users of nginx websites in mainland China include: Baidu, JD.com, Sina, NetEase, Tencent, Taobao, etc. It has a usage rate of 12.18% among the active websites in the world, which is about 22.2 million websites.

Nginx is a service with very simple installation, very concise configuration files (it can also support perl syntax), and very few bugs. Nginx is particularly easy to start, and it can run almost 7 * 24 without interruption, even if it runs for several months, it does not need to be restarted. You can also upgrade the software version without interruption of service.

The Nginx code is written entirely from scratch in C. Official data tests show that it can support responses up to 50,000 concurrent connections.

What does Nginx do?

reverse proxy

Reverse proxy (Reverse Proxy) means to accept the connection request on the Internet by proxy server, then forward the request to the server on the internal network, and return the result obtained from the server to the client requesting connection on the Internet, At this time, the proxy server acts as a reverse proxy server externally.

The reverse proxy serves the server. The reverse proxy can help the server receive requests from clients, help the server forward requests, load balance, etc.

The reverse proxy is transparent to the server and non-transparent to us, that is, we do not know that we are accessing a proxy server, but the server knows that the reverse proxy is serving him.
insert image description here

The difference between forward proxy and reverse proxy?

proxy object

Forward proxy, proxy client, the server does not know the client that actually initiated the request

Reverse proxy, proxy server, the client does not know the server that actually provides the service

architecture location

Forward proxy, set up between the client and the target host

Reverse proxy, set up on the server side

Different uses

The forward proxy is actually a proxy for the client, helping the client access server resources that it cannot access.

The reverse proxy is the proxy of the server, helping the server to do load balancing, security protection, etc.

load balancing

Load Balance (Load Balance) means to balance and distribute loads (work tasks, access requests) to multiple operating units (servers, components) for execution. It is the ultimate solution for high performance, single point of failure (high availability), and scalability (horizontal scaling).

Nginx load balancing algorithm

1. Polling (default)
Each request is allocated to different backend servers one by one in chronological order. If the maximum number of failures is exceeded (max_fails, default 1), within the failure time (fail_timeout, default 10 seconds), the node The failure weight becomes 0. After the failure time, it will return to normal, or after all nodes are down, then all nodes will be restored to valid and continue to detect. Generally speaking, rr can be evenly distributed according to the weight.

2.
The greater the value of the weighted training weight, the higher the probability of the assigned authority. It is mainly used for the uneven performance of each back-end server, or just to set different weight values ​​in the master-slave situation to achieve reasonable utilization of host resources.

3. least_conn The least connection
Prioritizes the scheduling of client requests to the server with the least current connections.

4.
Each request of ip_hash is assigned according to the hash result of the access ip, so that each visitor visits a backend server fixedly, which can solve the problem of session (session state), but ip_hash will cause uneven load, and some service requests accept more, Some service requests accept less, so it is not recommended to use ip_hash mode

Supplement: the concept of session

The session sharing problem can be used to replace the ip_hash of nginx with the session sharing of the back-end service. ip_hash cannot be applied to the four-layer reverse proxy, and the configuration needs to delete other modes such as weight and then add it directly

Notes:

Session, also known as session state, is the most commonly used state in web systems and is used to maintain some information related to the current browser instance. We often use Session to store user status in controlling user permissions

Session is actually divided into client-side Session and server-side Session.

When the user establishes a connection with the Web server for the first time, the server will distribute a SessionID to the user as an identifier. SessionID is a random string of 24 characters. Every time the user submits a page, the browser will include the SessionID in the HTTP header and submit it to the web server, so that the web server can distinguish which client is currently requesting the page. This SessionID is stored on the client and belongs to the client Session.

In fact, the client-side Session is stored in the form of cookies by default, so when the user disables cookies, the server-side cannot get the SessionID. At this time, we can use the url method to store the client Session. That is, the SessionID is written directly in the url. Of course, this method is not commonly used.

Session sharing: When a browser corresponds to multiple web services, the session data on the server needs to be shared. For example, scenarios such as single sign-on and web server clusters need to use multiple sub-services.

5. Fair (third party)
requests are allocated according to the response time of the back-end server, and those with short response times are given priority.

6. The url_hash (third-party)
algorithm is similar to the ip hash algorithm. It allocates each request according to the hash result of the URL, so that each URL is directed to the same backend server, but it will also cause the problem of uneven distribution. This mode It is better when the backend server is a cache.

static and dynamic separation

Separate dynamic requests from static requests. Dynamic requests access the tomcat server through nginx, and nginx can directly serve as a static request server. Static requests are directly processed by nginx, and dynamic requests are processed by tomcat.

Method to realize

  • Separate static resources into separate domain names and place them on separate servers, such as Qiniu Cloud;

  • Mixed release of dynamic and static resources, specify the suffix name of static resources through the nginx server, and realize the forwarding of different requests.

at processing.

Method to realize

  • Separate static resources into separate domain names and place them on separate servers, such as Qiniu Cloud;

  • Mixed release of dynamic and static resources, specify the suffix name of static resources through the nginx server, and realize the forwarding of different requests.

Guess you like

Origin blog.csdn.net/qq_29917503/article/details/131093824