An article to understand Nginx

Generation of Nginx

Nginx is a web server like Apache . Based on the REST architectural style, Uniform Resources Identifier URI or Uniform Resources Locator URL is used as the communication basis to provide various network services through HTTP protocol.

However, these servers were limited by the environment at the beginning of the design, such as the size of the users, network bandwidth, and product characteristics at that time, and their respective positioning and development were different. This also makes each Web server have its own distinctive characteristics.

Apache has a long history of development, and it is the undisputed world's largest server. It has many advantages: stable, open source, cross-platform, etc.

It has been around for too long. In the era of its rise, the Internet industry is far less than it is now. So it is designed as a heavyweight.

It does not support highly concurrent servers. Running tens of thousands of concurrent accesses on Apache will cause the server to consume a lot of memory.

The operating system switching between processes or threads also consumes a lot of CPU resources, resulting in a decrease in the average response speed of HTTP requests.

These all determine that Apache cannot become a high-performance Web server, and the lightweight and highly concurrent server Nginx came into being.

Russian engineer Igor Sysoev, who developed Nginx in C while working for Rambler Media.

As a web server, Nginx has always provided excellent and stable services for Rambler Media. Then, Igor Sysoev opened the Nginx code and gave it a free software license.

Due to the following points, Nginx is on fire:

 

  • Nginx uses an event-based architecture, which enables it to support millions of TCP connections.

  • The high degree of modularity and free software licenses have led to the emergence of third-party modules (this is an era of open source).

  • Nginx is a cross-platform server that can run on Linux, Windows, FreeBSD, Solaris, AIX, Mac OS and other operating systems.

  • Great stability brought by these excellent designs.

     

Where Nginx comes in

 

Nginx is a free, open source, high-performance HTTP server and reverse proxy server; it is also an IMAP, POP3, SMTP proxy server.

 

Nginx can be used as an HTTP server to publish the website. In addition, Nginx can be used as a reverse proxy for load balancing.

 

About the agent

 

Speaking of agency, we must first clarify a concept, the so-called agency is a representative, a channel; this time involves two roles, one is the role of the agent, the other is the target role.

 

The process that the agent role accesses the target role to complete some tasks through this agent is called the agent operation process; like the store in life, the customer buys a pair of shoes at the adidas store, this store is the agent, and the agent role is the adidas manufacturer, The target role is the user.

 

Forward agent

 

Before talking about reverse proxies, let ’s look at forward proxies. Forward proxies are also the most commonly encountered proxy modes. We will talk about the processing mode of forward proxies from two aspects, from the software and life aspects. Let me explain what a forward agent is.

 

In today's network environment, if we are going to visit certain websites abroad due to technical needs, then you will find that there is no way for us to visit a website located in a foreign country through a browser.

 

At this time, everyone may use an operation FQ to access. The way of FQ is to find a proxy server that can access foreign websites. We send the request to the proxy server. The proxy server visits the foreign website, and then passes the accessed data. Give us!

 

The above proxy mode is called forward proxy. The biggest feature of forward proxy is that the client knows the server address to be accessed very much; the server only knows which proxy server the request comes from, but not which specific client; forward proxy Modes block or hide real client information.

 

Let's look at a schematic diagram (I put the client and forward proxy together in the same environment, which I will introduce later):

 

 

The client must set a forward proxy server, of course, the premise is to know the forward proxy server's IP address, and the port of the proxy program.

 

As shown below:

 

In summary: the forward proxy, "it proxy is the client", is a server between the client and the original server (Origin Server), in order to obtain content from the original server, the client sends a request to the proxy and specifies Target (origin server).

 

The agent then forwards the request to the original server and returns the obtained content to the client. The client must make some special settings to use the forward proxy.

 

The purpose of the forward agent:

 

  • Access resources that were previously inaccessible, such as Google.

  • Can be used as a cache to speed up access to resources.

  • Authorize client access and surf the Internet.

  • The agent can record user access records (online behavior management) and hide user information from the outside.

 

Reverse proxy

 

Understand what is a forward proxy, we continue to look at the handling of reverse proxies Too.

 

At this time, a well-known term appeared: distributed deployment; that is, deploying multiple servers to solve the problem of limiting the number of visitors.

 

Most of the functions in a treasure website are also directly implemented using Nginx for reverse proxy, and after encapsulating Nginx and other components, it has a high name: Tengine.

 

Interested children's shoes can visit Tengine's official website to view specific information:

 

http://tengine.taobao.org/

 

 

So what kind of distributed cluster operation does the reverse proxy specifically implement? Let's first look at a schematic diagram (I put the server and reverse proxy box together and belong to the same environment, I will introduce later):

 

Through the above illustration, you can see clearly that the requests sent by multiple clients to the server are distributed by the Nginx server to the back-end business processing server for processing according to certain rules.

 

At this time, the source of the request, that is, the client is clear, but it is not clear which server the request is processed by. Nginx plays a reverse proxy role.

 

The client is a non-aware proxy, the reverse proxy is transparent to the outside, and the visitor does not know that he is accessing a proxy. Because the client can access without any configuration.

 

Reverse proxy, "It proxy is the server", mainly used in the case of distributed deployment of server clusters, reverse proxy hides the server information.

 

The role of reverse proxy:

 

  • To ensure the security of the intranet, the reverse proxy is usually used as the public network access address, and the web server is the intranet.

  • Load balancing, through a reverse proxy server to optimize the load of the website.

Project scene

 

Usually, when we are in actual project operation, the forward proxy and the reverse proxy are likely to exist in the same application scenario. The forward proxy proxy client requests to access the target server. The target server is a reverse proxy server. Reverse proxy has multiple real business processing servers.

 

The specific topology diagram is as follows:

Cut a picture to illustrate the difference between forward proxy and reverse proxy, as shown below:

 

Illustration:

 

  • In the forward proxy, Proxy and Client belong to the same LAN (in the box in the figure), hiding the client information.

  • In reverse proxy, Proxy and Server belong to the same LAN (in the box in the figure), hiding the server information.

 

In fact, what Proxy does in both types of proxy is to send and receive requests and responses on behalf of the server, but from the structural point of view, it is just about right and left, so the proxy method that appears later is called reverse proxy.

 

Load balancing

 

We have already clarified the concept of the so-called proxy server, then, Nginx plays the role of reverse proxy server, according to what rules does it distribute requests? For unused project application scenarios, can the distribution rules be controlled?

 

The number of requests sent by the client mentioned here and received by the Nginx reverse proxy server is what we call the load.

 

The number of requests is distributed according to certain rules, and the rules for processing to different servers are a kind of balancing rules.

 

Therefore, the process of distributing the requests received by the server according to the rules is called load balancing.

 

Load balancing In the actual project operation process, there are two types of hardware load balancing and software load balancing. Hardware load balancing is also called hard load, such as F5 load balancing, which is relatively expensive and expensive.

 

However, the stability and security of data are very well guaranteed, and companies such as China Mobile and China Unicom will choose hard loads to operate.

 

Considering the cost reasons, more companies will choose to use software load balancing. Software load balancing is a message queue distribution mechanism implemented using existing technology combined with host hardware.

 

The load balancing scheduling algorithm supported by Nginx is as follows:

 

①weight polling (default): the received requests are assigned to different back-end servers one by one in order, even if a back-end server goes down during use, Nginx will automatically remove the server from the queue and request acceptance Will not be affected in any way.

 

In this way, a weight value can be set for different back-end servers to adjust the allocation rate of requests on different servers.

 

The larger the weight data, the greater the probability of being assigned to the request; the weight value is mainly adjusted for different back-end server hardware configurations in the actual working environment.

 

②ip_hash: Each request is matched according to the hash result of the ip that initiated the client. Under this algorithm, a client with a fixed ip address will always access the same back-end server, which also solves the session in the cluster deployment environment to a certain extent. The problem of sharing.

 

③fair: Intelligently adjust the scheduling algorithm to dynamically allocate the time from the request processing of the back-end server to the response.

 

A server with a short response time has a high probability of allocating requests to a server with a high response time, and a server with a low response time has a low number of requests. It is a scheduling algorithm that combines the advantages of the first two.

 

However, it should be noted that Nginx does not support the fair algorithm by default. If you want to use this scheduling algorithm, please install the upstream_fair module.

 

④url_hash: Distribute requests according to the hash result of the accessed URL. Each requested URL will point to a fixed server in the back end, which can improve the cache efficiency when Nginx is used as a static server.

 

Also note that Nginx does not support this scheduling algorithm by default. To use it, you need to install the Nginx hash package.

 

Web server comparison

 

The comparison of several commonly used Web servers is as follows:

81 original articles published · Liked 69 · Visitors 50,000+

Guess you like

Origin blog.csdn.net/s2603898260/article/details/103849034