1. What is Nginx?

Nginx is a high-performance HTTP and reverse proxy server, as well as an IMAP/POP3/SMTP server

Nginx is a lightweight Web server/reverse proxy server and email (IMAP/POP3) proxy server is currently used by the most Web server or proxy server, such as Taobao, Sina, netease, Xunlei and so on are in use

2. Why Nginx?

Advantages:

  • Cross-platform and simple configuration
  • Non-blocking, high concurrent connections: handle 20,000 to 30,000 concurrent connections, official monitoring can support 50,000 concurrent connections
  • Low memory consumption: open 10 Nginx for 150M low memory cost: open source
  • Built-in health check: If one of the servers goes down, a health check is performed, and resent requests are not sent to the down server. Resubmit the request to another node.
  • Broadband savings: Supports GZIP compression and can add browser local cache
  • High stability: The probability of downtime is very small
  • Master /worker structure: a master process generates one or more worker processes
  • Receiving user requests is asynchronous: the browser sends the request to the Nginx server, which receives all the user requests first and then sends them to the backend Web server at once, greatly reducing the pressure on the Web server
  • It receives data from the Web server and sends it to the browser client
  • The network dependence is low. Load balancing can be performed as long as the ping succeeds
  • You can have multiple Nginx servers
  • Event-driven: Epoll model is used as the communication mechanism

3. Why is Nginx performing so well?

Benefit from its event processing mechanism: asynchronous non-blocking event processing mechanism: the use of epoll model, provide a queue, queue solution

4. How does Nginx achieve high concurrency

Service nginx start later, and then input # ps – ef | grep nginx, will find nginx has a master process and a number of worker processes, the worker processes are equal, are the master fork. In master, the socket (listenFD) that needs listen is first established, and then multiple worker processes are fork out. When users enter the nginx service, each worker’s listenfd becomes readable, and the workers compete for an accept_mutex. Accept_mutex is mutually exclusive. If one worker gets accept_mutex, the other workers stop working. The worker that receives the accept_mutex call starts “reading the request, parsing the request, and processing the request,” and the event ends once the data is returned to the client completely (the target page appears on the computer screen).

Nginx uses this method to meet the requirements of the underlying worker process. At the same time, it adopts an “asynchronous non-blocking” method to achieve high concurrency.

5. Why not use multithreading?

Because thread creation and context switching are very resource-intensive, thread memory consumption is high, and context switching CPU consumption is high, the epoll model avoids this disadvantage

6. How does Nginx handle a request?

First, when nginx starts up, it will parse the configuration file to find the port and IP address to listen on, and then in the master process of nginx

Initialize the socket(addrreuse, bind it to the specified IP address port, listen)

Then fork(an existing process can call the fork function to create a new process. New processes created by fork are called child processes

The child process then competes to accept the new connection. At this point, the client can initiate a connection to Nginx. When a client establishes a connection with Nginx after a three-way handshake

At this point, one of the child processes accepts the socket for the established connection, and then creates an NGINx encapsulation of the connection, called ngx_Connection_t

Next, set up read/write event handlers and add read/write events to exchange data with the client. Finally, nginx or the client takes the initiative to close the connection, at which point a connection dies

7. Forward proxy

A server located between the client and the origin server. In order to retrieve content from the origin server, the client sends a request to the agent and specifies the destination (the origin server).

The proxy then forwards the request to the original server and returns the obtained content to the client. The client can use the forward proxy

The forward proxy is summed up in one sentence: the proxy proxy is the client

8. Reverse proxy

In Reverse Proxy mode, a Proxy server receives Internet connection requests and sends the requests to servers on the Intranet

The proxy server acts as a reverse proxy server when the result obtained from the server is returned to the Internet client

Reverse proxy is summed up in one sentence: the proxy proxy is the server

9. Separation of dynamic and static resources

Dynamic resource, static resource separation is to make dynamic web pages in the dynamic website according to certain rules to distinguish between constant resources and often changed resources, dynamic resources do a good job after splitting

We can do the cache operation according to the characteristics of static resources, which is the core idea of static site processing

Dynamic resource, static resource separation simple summary is: dynamic file and static file separation

10. Why do we need static and dynamic separation?

In our software development, some requests need background processing (such as.jsp,.do, etc.), and some requests do not need to go through background processing (such as CSS, HTML, JPG, JS files, etc.).

These files that do not need to go through background processing are called static files, or dynamic files. So we ignore static files in the background. I’m going to say, “I’m going to ignore static files in the background.

Of course this is possible, but the number of requests in the background increases significantly. When we have requirements on the response speed of resources, we should use this strategy to solve the problem

Dynamic and static separation Static resources (SUCH as HTML, JavaScript, CSS, IMG files) are deployed separately from background applications to improve the speed of users’ access to static code and reduce the access to background applications

Here we put the static resources into Nginx and forward the dynamic resources to the Tomcat server

11. Load balancing

Load balancing means that the proxy server evenly distributes the received requests to each server

Load balancing improves the response speed of servers, improves the access quality, and reduces the concurrency pressure of background servers


Original: Java Architecture Notes