Nginx concurrency optimization

Time:2021-7-22

1、 The necessity of tuning

I’ve been reluctant to write optimized content to share. I really don’t know how to write it. Because, if I don’t write well, I will be comforted by all kinds of people. Today, I finally wrote it, because there are too many people asking me in the group or sharing their experience, so I still follow.

Before we talk about tuning, we need to know why and the relationship between business operation and tuning.

  • Business operation: online business is running normally, carrying the company’s business.
  • Monitoring business: monitor online business through monitoring business to find problems in time.
  • Optimize business: through monitoring and analysis, find business problems or bottlenecks, timely adjust and optimize business or software.
  • Test optimization: after the optimization is completed, the existing optimization needs to be tested to ensure that the business is stable and efficient in the current optimization mode and can solve the current problems.

This is a process of business operation, and also a way of operation and maintenance to ensure business stability, high efficiency and high availability.

2、 Dimensions of tuning and divergent views

The article on tuning is the most difficult to write, because I can only tell you the tuning options, but I can’t tell you the specific threshold, because different businesses run on different machines and consume different resources; And because of different scenarios, the corresponding tuning items and thresholds are ever-changing. It’s just like you and your brother in the upper bunk have a cold, but the drugs you prescribe in the hospital are quite different. That’s why when many people see the article on tuning and see the specific tuning items or thresholds, two words will appear. I’m sorry to say, let’s match it with a graph! It’s just as well that we all agree.

3、 Nginx tuning

  • Nginx concurrency
  • Nginx process optimization
  • Nginx compression
  • Nginx local cache

1. Nginx concurrency number setting

worker_processes 1;

This is used to configure nginx to start several work processes. The default value is 1. Nginx also supports a worker_ cpu_ Affinity configuration item, that is, nginx can bind CPU for each worker process. I made the following configuration:


 events {
    worker_connections 1024;
}

2. Nginx binds the process to a fixed core

worker_cpu_affinity 0010 0100 1000;

worker_processes 3;
worker_cpu_affinity 0010 0100 1000;
#Here, 0010, 0100 and 1000 are masks, representing the second, third and fourth CPU cores respectively.

After restarting nginx, each of the three working processes can use its own CPU.

PS – EO, PSR, PID, Arg view

Nginx concurrency = worker_ processes*worker_ connections

3. Nginx compression function

Compression function is too important for web server, mainly reflected in the following two directions:

1) improve the user experience: the transmission data becomes smaller and the waiting time of users becomes shorter.

2) save the company’s bandwidth cost: after compression, the transmission data becomes smaller and the bandwidth is occupied less.

Since it can give users a good experience, it can also save money for the company. Why not do such a good thing? So this is a necessary artifact in work.

However, we need to pay more attention to the configuration compression:

1) do not compress pictures, audio and video

2) do not compress if the pressure is less than 1K, otherwise the greater the pressure is

3) the higher the compression level, the more CPU will be consumed

Compression not on

Turn on compression

The code is as follows:

gzip on;   ( Enable gzip compression)

gzip_ http_ version 1.1;  Its default value is http / 1.1, which means that the HTTP / 1.1 request will be gzip compressed

gzip_ disable "MSIE [1-6]."; The setting is to disable gzip compression in ie1-6

gzip_ proxied any; ( When nginx is used as the front-end agent, it means that compression is enabled unconditionally no matter what information is returned by the headers header of the back-end server.)

gzip_ min_ length 1024; ( The minimum compression page, if the page is too small, may be more and more compression, here the stipulation is greater than 1K page only to enable compression)

gzip_ buffers   4 8k; ( Set the system to get several units of cache to store gzip compression results. The data stream applies for 4 times of memory space in 8K units according to the original data size.)

gzip_ comp_ level 3; ( Compression level: 1 compression ratio is the smallest, processing speed is the fastest, 9 compression ratio is the largest, but processing speed is the slowest, and CPU consumption is the most, generally set to 3)

gzip_ types    text/plain text/css application/x-javascript application/javascript application/xml; ( What type of page or document is compression enabled

Turn on compression verification

4. Nginx local cache function

Browser caching is to speed up browsing and save network resources. The browser stores the recently requested documents on the user’s disk.

There is an essential difference between client caching and compression. After the user downloads the data for the first time, it will be saved on the client’s local hard disk. Next time, as long as the local resources are not expired, it will be read directly from the local hard disk. The speed is the fastest, because there is no need to go to the web server to get the data. Again, it optimizes the user experience and saves company bandwidth costs

It should be noted that:

Caching generally caches images, website frames, audio and video data that don’t change often. The best application is Baidu home page. Do you find that sometimes you can open Baidu home page without network, that is to see your local cache.

Nginx local cache configuration instruction

Nginx can set the browser’s header through the expires command

Syntax: expires [time|epoch|max|off]

Default: expires off

Scope: http, server, location

Use this command to control the headers of "expires" and "cache control" in HTTP response (to control page caching).
You can use positive or negative numbers in the time value“ The value of the "expires" header will be obtained by adding the current system time to the time value you set.

Epoch specifies that the value of "expires" is 1 January, 1970, 00:00:01 GMT.
Max specifies that the value of "expires" is 31 December 2037 23:59:59 GMT, and the value of "cache control" is 10 years.
-1 specifies that the value of "expires" is the current server time of - 1s, that is, it will expire forever

Cache case

Image cache for 30 days
location ~.*\.(jpg|png|jpeg)$
  {
 expires 30d;
 }

JS CSS cache for one hour
location ~.*\.(js|css)?$
  {
 expires 1h;
 }

Cache validation

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.