About the author: Mu Ke, former Alibaba operation and maintenance expert


This text is from: Hook Education
36 Exercises of operation and Maintenance Master

I’m Jeson, today, to share with you: about Nginx configuration optimization content, I believe you must be familiar with Nginx, it is a lightweight open source Web service and proxy program.

Nginx is widely used by a large number of enterprises in China due to its lightweight, high concurrency and other features. Since Nginx is so widely used, how to optimize and configure it becomes a priority in our operations and maintenance work. Today we are going to focus on basic configuration optimization for Nginx.

01 CPU affinity

What does CPU affinity do? Today’s CPU is usually multi-core, and can be virtualized through hyperthreading more cores, that affinity is multi-core CPU to make Nginx service fully used, so as to improve performance.

When Nginx runs, one master process and multiple worker processes will be enabled. The worker process is responsible for processing requests. If the worker process occurs frequent scheduling in multi-core CPU, performance will be lost.

In this case, we want to reduce frequent scheduling so that each Nginx worker thread can be fixed to a specific CPU core, so we need to configure Nginx CPU affinity to solve this problem.

Nginx CPU affinity configuration has multiple configuration schemes. It is recommended that you directly set the configuration item to auto (worker_CPU_affinity), which adopts the CPU core binding policy recommended by Nginx.

Another way is manual binding, setting the number of worker threads and the number of CPU cores one by one. We set it to Auto and Nginx automatically recognizes and allocates worker threads and cpus according to the recommended policy.

As shown in the figure, we can see that there are 8 CPU cores, namely CPU0~CPU7. If we set auto, Nginx will bind the 8 worker threads to the CPU core in a one-to-one manner according to the recommended policy. This avoids frequent scheduling of worker threads by the CPU, thus reducing CPU consumption.

This article is selected from the 36 Exercises of operation and Maintenance Masters by Laggo Education.

02 I/O flow event model

The second basic configuration optimization for Nginx is the Epoll stream event model. By default, Nginx uses the epoll stream event model. So why does Nginx choose to use epoll? This is because epoll has the following advantages. Let’s break it down:

First of all, on Linux everything is a file, so if we open a device, it will generate a file descriptor. When a process is generated, the process needs a process descriptor, which is also a file. So when Nginx processes a request, each request generates a descriptor for processing the request.

Second, what does epoll have to do with Nginx’s need to adopt an asynchronous non-blocking model to improve concurrency efficiency when handling large requests? Epoll itself is an asynchronous non-blocking model to handle the flow of events in the request flow.

It is important to note that not all Linux operating systems can use epoll. Epoll was introduced after kernel 2.6. The select\poll model used by earlier kernels is much lower in performance than epoll model. Experienced operation and maintenance students must have a deep experience.

Through the above background, we will introduce the advantages of epoll compared with select model in detail:

  • The epoll handling event flow model is thread-safe;
  • Compared with select model, epoll uses Mmap to share user and kernel space when calling FD file descriptor, which improves efficiency.
  • Epoll is event-driven. Compared with SELECT, which requires scanning the state of the entire file descriptor, ePoll avoids frequent scanning of file descriptors and can directly call the callback function, which is more efficient.
  • Removed the maximum limit (1024) on the number of file descriptors that can be monitored by a single process in the Select model. If you have used earlier versions of Apache, which used the Select model, delays or request errors occurred after 1000 requests. Switching to Nginx will significantly improve performance.

Worker_connections = worker_connections = worker_connections = worker_connections = worker_connections = worker_connections = worker_connections The default is 1024.

In high concurrency scenarios, 1024 for a single worker thread is often too low. It is recommended that you set worker_connections to a higher value, depending on the actual business maximum Nginx processing peak value.

03 zero copy

The so-called zero-copy configuration is to add a sendFile on configuration item to the HTTP configuration module in Nginx, and it is a zero-copy. The so-called zero-copy does not mean that no copy is made, but that it makes zero copy of the file from the kernel state to the user state.

As shown, let’s first look at what file transfer looks like without zero copy. First, when processing files, Nginx will pass the files to the kernel Buffer Cache of the operating system, and then pass them to the user state of the upper layer of the operating system, and then pass them back to the kernel state through the user state Buffer Cache, and finally forward the files out through the Socket.

This time you will find a question for a static file does not need to transfer to the user mode, directly through the kernel mode is more effective, so we will need to open the sendfile on in Nginx, such static files can be finished with a red path in kernel mode forwarding, and don’t have to bypass user mode, improve the efficiency.

This article is selected from the 36 Exercises of operation and Maintenance Masters by Laggo Education.

04 File Compression

The last step in the Nginx infrastructure optimization is file compression. We want to make sure that the less data the Nginx server sends to the client, the lower the latency, and the better the user experience. Therefore, file compression is usually set in the proxy or Nginx. We mainly use gzip to set the main Settings as follows:

  • Gzip on is responsible for enabling the compression function of the back end;
  • Gzip_buffer 16 8K sets the memory size of Nginx for file compression.
  • Gzip_comp_level 6 indicates the compression level of Nginx when handling compression. Usually, the higher the compression level, the higher the compression ratio, the better it is. It is still necessary to choose the appropriate compression ratio according to the actual situation. It is generally recommended that you set it to 6;
  • Gzip_http_version 1.1 indicates that only HTTP 1.1 protocols are compressed.
  • Gzip_min_length 256 indicates that compression is performed only when the length is larger than the minimum length of 256 bytes. If the length is smaller than the minimum length of 256 bytes, compression is not performed.
  • Gzip_proxied any indicates that when Nginx acts as a reverse proxy, it sets some GZIP compression policies based on the information returned by the back-end server.
  • Gzip_vary on indicates whether to send the Vary: Accept_Encoding response header field to notify the receiver of gZIP compression on the server.
  • application/vnd.ms-fontobject image/x-icon; Gip compression type
  • Gzip_disable msie6; Turn off compression in IE6

The last two items indicate setting the zip compression type and whether to disable the compression requested by the client using Internet Explorer 6. This is a typical configuration of file compression, and you can make some adjustments based on your specific situation.

Ok, that’s all for today. Next class I’ll talk about cache configuration optimization. Be sure to come to class on time. Pay attention to my public number: IT technology thinking, reply: 123, you can get free big factory interview real questions oh ~

This article is selected from the 36 Exercises of operation and Maintenance Masters by Laggo Education.

Copyright notice: The copyright of this article belongs to Pull hook education and the columnist. Any media, website or individual shall not be reproduced, linked, reposted or otherwise copied and published/published without the authorization of this agreement, the offender shall be corrected.