Nginx worker_processes optimization, worker_processes
Nginx worker_processes Parameters
Source: http://bbs.linuxtone.org/thread-1062-1-1.html
Sharing 1:
If you find the original author:
As a general rule you need the only worker with large number
Worker_connections, say 10,000 or 20,000.
However, if nginx does CPU-intensive work as SSL or gzipping and
You have 2 or more CPU, then you may set worker_processes to be equal
To CPU number.
Besides, if you serve your static files and the total size of the files
Is bigger than memory, then you may increase worker_processes
Utilize a full disk bandwidth.
Igor Sysoev
Generally, a process is enough. You can set a large number of connections.
If there are SSL and gzip jobs that consume CPU resources and are multi-core CPUs, you can set it to the same number as the CPU.
You can also increase the number of processes when processing many small files and the total file size is much larger than the memory size,
To make full use of the I/O bandwidth (it seems that I/O operations have blocks ).
Based on my configuration practices,
The server is an environment where the total file size of multiple CPUs + gzip + websites is greater than the memory. It is better to set worker_processes to twice the number of CPUs.
Share 2:
Recently, a 502 error occurs in PPC, and web pages cannot be opened frequently. Therefore, I decided to make an in-depth effort on Nginx!
Nginx is not suspended. Otherwise, error 502 is not displayed, so the reason must be the Nginx settings.
After reading the materials and testing, I found that it may be caused by improper parameter settings of worker_processes.
Worker_processes is 1 by default. Generally, you do not need to modify it. However, you can modify this value to improve performance;
The official suggestion is to change the number of cores to the CPU. Here we reference a translated article:
Worker_processes indicates the number of processes to be enabled by nginx,
According to the official statement, it is enough to open one, but a few more can reduce the impact of machine io.
According to the practice, this nginx parameter can be opened with four or eight in general, and the optimization is not very large if it is opened up.
According to another statement, too many processes are enabled in nginx, which affects the scheduling of the main process, so the cpu usage increases.
After my tests, I found that,
This number cannot be set randomly. If the website does not have any I/O performance problems, it is best not to modify it. Use the default value of 1,
If you have to set it, it must match the number of CPU cores; otherwise, it will either be suspended (mainly in Windows) or 502 (mainly in Linux ).
My computer is dual-core. It should be 2, but it should be 4, because it is dual-threaded. The test results are as follows:
1. The value of worker_processes is 1, and there are two open threads, one of which is the main thread and runs very stably.
2. The value of worker_processes is 2, and three threads are opened. One of them is the main thread, which crashes in about 1 minute.
(The web page cannot be opened, and the browser is always loading ).
3. When worker_processes is 4, five threads are opened, and one of them is the main thread, which runs very stably.
4. The value of worker_processes is 8, and nine threads are opened. One of them is the main thread, which is the same as 2 and crashes in about 1 minute.
Configuration reference
Configuration 1:
4 CPU (4 Core) + 4 worker_processes (each worker_processes uses 1 CPU)
Reistlin@reistlin.com ~ $ Cat/proc/cpuinfo | grep processor
Processor: 0
Processor: 1
Processor: 2
Processor: 3
Worker_processes 4;
Worker_cpu_affinity 0001 0010 0100 1000;
Configuration 2:
8 CPU (8 Core) + 8 worker_processes (each worker_processes uses one CPU)
Reistlin@reistlin.com ~ $ Cat/proc/cpuinfo | grep processor
Processor: 0
Processor: 1
Processor: 2
Processor: 3
Processor: 4
Processor: 5
Processor: 6
Processor: 7
Worker_processes 8;
Worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
Configuration 3:
16 CPU (16 Core) + 16 worker_processes (each worker_processes uses one CPU)
Reistlin@reistlin.com ~ $ Cat/proc/cpuinfo | grep processor
Processor: 0
Processor: 1
Processor: 2
Processor: 3
Processor: 4
Processor: 5
Processor: 6
Processor: 7
Processor: 8
Processor: 9
Processor: 10
Processor: 11
Processor: 12
Processor: 13
Processor: 14
Processor: 15
Worker_processes 16;
Worker_cpu_affinity
0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000;
Nginx worker_cpu_affinity settings
According to the information on Nginx Wiki:
Worker_cpu_affinity
Syntax: worker_cpu_affinity cpumask [cpumask...]
Default: none
Linux only.
With this option you can bind the worker process to a CPU, it callsched_setaffinity ().
For example,
Worker_processes 4; worker_cpu_affinity 0001 0010 0100;
Bind each worker process to one CPU only.
Worker_processes 2; worker_cpu_affinity 0101 1010;
Bind the first worker to CPU0/CPU2, bind the second worker to CPU1/CPU3. This is suitable for HTT.
Worker_cpu_affinity is disabled by default,
Based on the example, we can see that 0001 0010 0100 1000 represents 1st, 2, 3, and 4 logical CPUs,
Therefore, we can set 0010 0100 1000 to bind three processes to 2nd, 3, and 4 logical CPUs respectively:
Worker_processes 3;
Worker_cpu_affinity 0010 0100 1000;
We can also see from the example that,
Worker_cpu_affinity can bind the same process to two logical CPUs:
Worker_processes 2;
Worker_cpu_affinity 0101 1010;
0101 is 1st and 3 logical CPUs, and 1010 is 2nd and 4 logical CPUs.
Share 3:
Worker_processes indicates the number of processes to be enabled by nginx,
According to the official statement, it is enough to open one, but a few more can reduce the impact of machine io.
According to the practice, this nginx parameter can be opened with four or eight in general, and the optimization is not very large if it is opened up.
According to another statement, too many processes are enabled in nginx, which affects the scheduling of the main process, so the cpu usage increases,
I personally did not confirm this statement. It is estimated that they started 100 or 200 processes for comparison.
Precautions for worker_processes Configuration:
1. It is best to write the worker_cpu_affinity Configuration
Most of my servers here are dual-core hyper-threading, which is equivalent to 4 CPUs. I usually run 8 processes, so this configuration is like this:
Worker_cpu_affinity 0001 0100 1000 0010 0001 0100 1000 0010;
In addition, worker_cpu_affinity is not always usable,
I did not carefully study and list all the situations, but I only knew that machines with the 2.4 kernel could not be used,
If it cannot be used, it is best to increase the number of worker_processes processes so that the cpu allocation will average a bit,
If not, you have to restart a few more times.
2. The worker_rlimit_nofile configuration must be consistent with the number of files opened by a single process in the system. Do not overwrite them by worker_processes.
In linux 2.6, the number of opened files is 65535, and 65535 is required for worker_rlimit_nofile.
This is because the allocation of requests to processes during nginx scheduling is not so balanced, so if you enter 10240,
When the total concurrency reaches 3-4, 10240, a process may exceed 502, and a error will be returned.