What is nginx? What can we do?


What is nginx? What can I do

The production of nginx

Never heard of nginx? So you must have heard of its “peer” Apache! Nginx, like Apache, is a kind of web server. Based on the rest architecture style, the uniform resources identifier (URI) or uniform resources locator (URL) is used as the communication basis to provide various network services through HTTP protocol.

However, at the beginning of the design, these servers were limited by the environment at that time, such as the size of users, network bandwidth, product characteristics and so on, and their positioning and development were not the same. This also makes each web server has its own distinctive characteristics.

Apache has a long history of development, and it is the world’s largest server without dispute. It has many advantages: stable, open source, cross platform and so on. It has been around for a long time. The Internet industry is far less developed than 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. The switching between processes or threads by the operating system also consumes a lot of CPU resources, resulting in the reduction of the average response speed of HTTP requests.

All these make Apache impossible to be a high-performance web server, so nginx, a lightweight and high concurrency server, came into being.

Igor sysoev, a Russian engineer, 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 will open source nginx code and grant free software license.


  • Nginx uses an event driven architecture to support millions of TCP connections
  • High modularity and free software license make the third-party modules emerge in endlessly
  • Nginx is a cross platform server, which 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 to use nginx

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 publishing processing, and nginx can be used as a reverse proxy for load balancing.

About agency

When it comes to agency, first of all, we need to define a concept. The so-called agency is a representative and a channel;

At this time, it involves two roles, one is the agent role, and 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. It’s just like the monopoly store in life, when a customer buys a pair of shoes in Adidas monopoly store, the monopoly store is the agent, the agent role is Adidas manufacturer, and the target role is the user .

Forward agency

Before we talk about reverse proxy, let’s take a look at forward proxy. Forward proxy is also the most frequently contacted proxy mode. We will explain the processing mode of forward proxy from two aspects: software and life.

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

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

What is nginx? What can we do?

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.

What is nginx? What can we do?

To sum up: a forward proxy, “it acts as a proxy for the client and sends requests on behalf of the client”, is a proxy between the client and the original server In order to get the content from the original server, the client sends a request to the agent and specifies the target (the original server), then the agent 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.

Purpose of forward proxy:
(1) Access to previously inaccessible resources, such as Google
(2) Can do cache, accelerate access to resources
(3) The client access authorization, online authentication
(4) Agents can record user access records (Internet behavior management) and hide user information

Reverse proxy

Now that we understand what forward proxy is, let’s continue to look at the processing methods of reverse proxy. For example, the number of visitors who connect to the website at the same time every day has exploded, and a single server is far from meeting the people’s growing desire to buy. At this time, there is a familiar term: distributed deployment; that is, through the Department Many servers are deployed to solve the problem of limited number of visitors; most of the functions of a treasure website are directly implemented by using nginx as reverse proxy, and after encapsulating nginx and other components, it has a big name: Tengine. If you are interested in children’s shoes, you can visit Tengine’s official website to see the specific information:http://tengine.taobao.org/. So what is the specific way to realize the distributed cluster operation of reverse proxy? Let’s first look at a schematic diagram (I frame the server and reverse proxy together and belong to the same environment, which I will introduce later)

What is nginx? What can we do?

Through the above diagram, you can see clearly that after the nginx server receives the requests sent by multiple clients to the server, it distributes them 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 handles the request. Nginx plays a reverse proxy role.

The client is not aware of the existence of the agent, the reverse agent is transparent to the outside, visitors do not know that they are visiting an agent. Because the client does not need any configuration to access.

Reverse proxy “it represents the server and receives requests on behalf of the server”. It is mainly used in the case of distributed deployment of server cluster. Reverse proxy hides the information of server.

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

Project scenarios

In general, when we operate the actual project, the forward proxy and the reverse proxy are likely to exist in an application scenario. The forward proxy proxy proxy clients’ requests to access the target server. The target server is a reverse simple server, which reversely proxy multiple real business processing servers. The specific topology is as follows:

What is nginx? What can we do?

The difference between the two

Cut a figure to illustrate the difference between forward proxy and reverse proxy, as shown in the figure.

What is nginx? What can we do?


In forward proxy, proxy and client belong to the same LAN (in the box in the figure), which hides 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 the two kinds of proxy is to send and receive requests and responses for the server, but from the perspective of structure, it just interchanges left and right, so the later proxy mode is called reverse proxy.

load balancing

We have made clear the concept of the so-called proxy server. Next, nginx acts as a reverse proxy server. According to what rules does it distribute requests? Can the rules of distribution be controlled for different project application scenarios?

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 according to certain rules is a kind of balance 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, there are two kinds of load balancing: Hardware load balancing and software load balancing. Hardware load balancing is also known as hard load, such as F5 load balancing, which is relatively expensive and high cost, but the stability and security of data are very well guaranteed. Companies like China Mobile and China Unicom will choose hard load for operation; more companies will take the test Considering the cost, we will choose to use software load balancing, which is a message queue distribution mechanism based on existing technology and host hardware.

What is nginx? What can we do?

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

  1. Weight polling (default, commonly used, with HA function!) : the received requests are assigned to different back-end servers according to the weight. Even if a back-end server is down during use, nginx will automatically remove the server from the queue, and the request acceptance will not be affected. 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 allocated to requests; the weight value 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 can visit a back-end server, which also solves the problem of session sharing in the cluster deployment environment to a certain extent.
  3. Fair: intelligent adjustment scheduling algorithm, dynamic according to the back-end server request processing to response time for balanced allocation, short response time, high processing efficiency of the server allocated to the request probability is high, long response time, low processing efficiency of the server allocated to the request less; a scheduling algorithm combining the advantages of the former two. However, it should be noted that nginx does not support fair algorithm by default. If you want to use this scheduling algorithm, please install upstream_ Fair module.
  4. url_ Hash: requests are allocated according to the hash result of the accessed URL. The URL of each request will point to a fixed server on 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 nginx’s hash package.

Comparison of several common web servers

Comparison item / server Apache Nginx Lighttpd
Proxy proxy very nice very nice commonly
Rewriter good very nice commonly
Fcgi Not good good very nice
Hot deployment I won’t support it support I won’t support it
system pressure great It’s very small It’s small
stability good very nice Not good
Security good commonly commonly
Static file processing commonly very nice good
Reverse proxy commonly very nice commonly

So far. Reprint please indicate the source, remember to scan code reward support Oh, thank you!


What is nginx? What can we do?