Introduction to Nginx (What is Nginx? What can it do?)

Generation of Nginx

  Never heard of Nginx? Then you must have heard of its "peer" Apache! Like Apache, Nginx is a WEB server. Based on the REST architecture style, it uses Uniform Resources Identifier (Uniform Resources Identifier) ​​URI or Uniform Resources Locator (Uniform Resources Locator) URL as the communication basis, and provides various network services through the HTTP protocol. .

  However, these servers were limited by the environment at the beginning of design, such as the user scale, network bandwidth, and product features at that time, and their positioning and development were different. This also makes each WEB server have their own distinct characteristics.

  Apache's development period is very long, and it is the undisputed number one server in the world. It has many advantages: stability, open source, cross-platform and so on. It has been around for too long, and in the era when it emerged, the Internet industry was far inferior to what it is now. So it was designed to be a heavyweight. It is a server that does not support high concurrency. Running tens of thousands of concurrent accesses on Apache will cause the server to consume a lot of memory. Switching between processes or threads by the operating system also consumes a large amount 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 high-concurrency 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 been providing excellent and stable services for Rambler Media.

Then, Igor Sysoev open sourced the Nginx code and gave it a free software license.

  because:

  • Nginx uses an event-driven architecture that enables it to support millions of TCP connections
  • A high degree of modularity and free software licenses make third-party modules emerge in endlessly (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
  • These excellent designs bring great stability

  So, Nginx is on fire!

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 for website release processing, and Nginx can be used as a reverse proxy server The proxy implements load balancing.

About the agent

  When it comes to agency, we must first clarify a concept, the so-called agency is a representative and a channel;

  At this time, two roles are involved, one is the proxy role, and the other is the target role. The process of the proxy role accessing the target role through this proxy to complete some tasks is called the proxy operation process; it is like a specialty store in life~customers go to adidas When a specialty store buys a pair of shoes, the specialty store is the agent, the agent role is the adidas manufacturer, and the target role is the user.

forward proxy

  Before talking about the reverse proxy, let’s take a look at the forward proxy. The forward proxy is also the proxy mode that everyone comes into contact with the most. We will talk about the processing mode of the forward proxy from two aspects, from the software aspect and the life Let me explain what a forward proxy is.

  In today’s network environment, if we want to visit some foreign websites due to technical needs, you will find that we cannot access a foreign website through a browser. At this time, everyone may use an operation FQ To access, the way of FQ is mainly to find a proxy server that can access foreign websites, we will send the request to the proxy server, the proxy server will visit foreign websites, and then pass the accessed data to us!

  The above proxy mode is called forward proxy. The biggest feature of forward proxy is that the client is very clear about the address of the server to be accessed ; the server only knows which proxy server the request comes from, but not which specific client it comes from; forward proxy Patterns mask or hide real client information . Let’s look at a schematic diagram (I frame the client and the forward proxy together, which belong to the same environment, and I will introduce it later):

  The client must set up a forward proxy server , of course, the premise is to know the IP address of the forward proxy server and the port of the proxy program. As shown in the picture.

  To sum up: Forward proxy, "it acts as the client and sends requests on behalf of the client", is a server located between the client and the original server (origin server), in order to obtain content from the original server, the client sends to The proxy sends a request and specifies the target (origin server), then the proxy forwards the request to the origin 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 proxy:
(1) Access resources that cannot be accessed before, such as Google
(2) It can be used as a cache to speed up access to resources
(3) Authorize client access and authenticate the Internet
(4) The proxy can record user access records ( Internet behavior management), hide user information from the outside world

reverse proxy

  After understanding what a forward proxy is, let’s continue to look at how to deal with reverse proxy. For example, a certain treasure website of My Great Heavenly Dynasty, the number of visitors who connect to the website at the same time every day has exploded, and a single server is far from being able to meet the growing needs of the people. The desire to buy, at this time, a familiar term appeared: distributed deployment; that is, to solve the problem of limiting the number of visitors by deploying multiple servers; most of the functions of a treasure website are also directly implemented by reverse proxy using Nginx Yes, and after encapsulating Nginx and other components, it has a tall name: Tengine. Interested children's shoes can visit Tengine's official website to view specific information: http://tengine.taobao.org/. So what kind of method does the reverse proxy realize the distributed cluster operation? Let's look at a schematic diagram first (I frame the server and the reverse proxy together, and they belong to the same environment. I will introduce it later):

  Through the above illustration, you can see clearly that after receiving the requests sent by multiple clients to the server, the Nginx server distributes them to the back-end business processing server according to certain rules for processing. At this time ~ the source of the request is the client is clear, but it is not clear which server the request is processed by. Nginx plays the role of a reverse proxy.

  The client is unaware of the existence of the proxy, and the reverse proxy is transparent to the outside world, and the visitor does not know that he is visiting a proxy. Because the client can access without any configuration.

  Reverse proxy, "it acts as a proxy for the server and receives requests on behalf of the server" , is mainly used in the case of distributed deployment of server clusters, and the reverse proxy hides the information of the server.

  The role of reverse proxy:
(1) To ensure the security of the intranet, usually the reverse proxy is used as the public network access address, and the web server is the intranet
(2) Load balancing, optimize the load of the website through the reverse proxy server

project scene

  Usually, when we operate the actual project, the forward proxy and reverse proxy are likely to exist in an application scenario. The forward proxy acts as a proxy for the client's request to access the target server. The target server is a reverse simple interest server. , reverse proxy multiple real business processing servers. The specific topology diagram is as follows:

difference between the two

  A picture was taken to illustrate the difference between forward proxy and reverse proxy, as shown in the figure.

 

In the forward proxy, the Proxy and Client belong to the same LAN (in the box in the figure), which hides the client information;

In the reverse proxy, Proxy and Server belong to the same LAN (in the box in the figure), which hides the server information;

In fact, what Proxy does in the two kinds of proxies is to replace the server to send and receive requests and responses, but from the structural point of view, it happens to be interchanged, so the proxy method that appeared later is called reverse proxy.

load balancing

  We have clarified the concept of the so-called proxy server, so next, Nginx plays the role of a reverse proxy server. What rules does it use to distribute requests? For different project application scenarios, can the distribution rules be controlled?

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

  The rule that the number of requests is distributed to different servers for processing according to certain rules is a kind of balancing rule.

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

  In the actual project operation process of load balancing, 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, but the stability and security of data, etc. There are very good guarantees. Companies such as China Mobile and China Unicom will choose hard loads to operate; more companies will choose to use software load balancing for cost reasons. Software load balancing uses existing technologies combined with host hardware A message queue distribution mechanism implemented.

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

  1. Weight polling (default, commonly used, with HA effect!): Received requests are assigned to different backend servers according to their weights. Even if a backend server goes down during use, Nginx will automatically remove the server Dequeue, the request acceptance will not be affected in any way. In this way, you can set a weight value (weight) for different back-end servers to adjust the allocation rate of requests on different servers; the greater the weight data, the greater the probability of being allocated to the request; the weight value, It is mainly adjusted for different back-end server hardware configurations in the actual working environment.
  2. ip_hash (commonly used): Each request is allocated according to the hash result of the access ip, so that each visitor accesses a backend server fixedly, which also solves the problem of session sharing in a cluster deployment environment to a certain extent.
  3. fair: intelligently adjust the scheduling algorithm, and dynamically balance the allocation according to the time from request processing to response of the back-end server. Servers with short response time and high processing efficiency have a high probability of being allocated to requests, and servers with long response time and low processing efficiency are allocated. Fewer requests; a scheduling algorithm that combines the advantages of the former 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.
  4. url_hash: Allocate requests according to the hash result of the accessed url. The url of each request will point to a fixed server at the backend, 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. If you want to use it, you need to install the hash package of Nginx.

 Comparison of several commonly used web servers

Compare item\server Apache Nginx Lighttpd
Proxy proxy very good very good generally
Rewriter good very good generally
Fcgi not good good very good
hot deployment not support support not support
System pressure very big very small smaller
stability good very good not good
safety good generally generally
Static file handling generally very good good
reverse proxy generally very good generally

Guess you like

Origin blog.csdn.net/weixin_45925028/article/details/132316235