Optimize Nginx service performance based on parameters

Reference material

[1] . learn Linux operation and maintenance from the old boy: Web Cluster practice, old boy

setup script

Number of worker processes optimizing Nginx service

The configuration parameters of the Nginx service corresponding to the optimized Nginx process are as follows

Worker? Processes 1;? Specifies the number of processes to be opened by Nginx, and the number at the end is the number of processes.

The above parameters adjust the number of worker processes of nginx service. Nginx is divided into master process and nginx process. Master is the management process, and the worker process is really responsible for external.

The strategy of optimizing the number of Nginx processes

In general, the initial setting of the number of worker processes can be equal to the number of CPU cores, and the number of worker processes is more. In the case of high traffic and high concurrency, it can also be considered to increase the number of processes to the number of CPU cores * 2.

View Web server CPU hardware resource information

## View the total number of CPU cores through / proc/cpuinfo
[root@www ~]# grep processor /proc/cpuinfo|wc -l
[root@www ~]# grep -c processor /proc/cpuinfo 

## View the total number of CPU s
[root@www ~]# grep 'physical id' /proc/cpuinfo|sort|uniq|wc -l

## With the top command, press the number 1 to display all the CPU cores
[root@www ~]# cpu
## Display the following information
top - 12:09:10 up 12 min,  1 user,  load average: 0.00, 0.00, 0.00
Tasks:  75 total,   1 running,  74 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1906276k total,   172244k used,  1734032k free,    10888k buffers
Swap:  1048572k total,        0k used,  1048572k free,    40872k cached
... ...

Practice modifying Nginx configuration

## Check the number of workers and processes in nginx.conf
[root@www ~]# grep worker_processes /application/nginx-1.9.9/conf/nginx.conf
## Modify the parameter value to the total number of CPU cores 4, and then reload the Nginx service
[root@www ~]# sed -i 's#worker_processes  1#worker_processes  4#g'  /application/nginx-1.9.9/conf/nginx.conf
## View modification results
[root@www ~]# grep worker_processes /application/nginx-1.9.9/conf/nginx.conf
## Restart Nginx
[root@www ~]# /application/nginx/sbin/nginx -t
[root@www ~]# /application/nginx/sbin/nginx -s reload
## Check the number of modified worker processes
[root@www ~]# ps -ef|grep nginx|grep -v grep

//The official description of the worker? Process parameter is as follows:

 syntax:        worker_processes  number;   #This behavior parameter syntax, number is quantity
default:    worker_processes  1;    #This line means that the parameter is not configured, and the software default is 1
context:    main    #This line is where the worker? Processes parameter can be placed
worker_processes To define worker Number of processes, recommended to CPU Or cpu Kernel number*2The number of processes should be selected according to the actual business. In addition to CPU In addition to the matching of cores, it is also related to the data stored in the hard disk and the load of the system, which is set to CPU Is a good starting configuration
//Click to see more official details

Tags: Nginx Linux Web Server

Posted on Sun, 05 Apr 2020 12:20:15 -0700 by nelson201