Nginx the number of server processes to set up and utilize multi-core CPUs _nginx

Source: Internet
Author: User
Tags mysql query

Nginx configuration file nginx.conf

First you need to find the Nginx profile nginx.conf in order to do the following, under the LNMP one-click installation package default configuration, nginx.conf stored in the

/usr/local/nginx/conf/nginx.conf

For other environments, installation Nginx can be used

Find/-name nginx.conf

To find the location of the configuration file.

Nginx worker_processes Process Number setting

Worker_processes can be set to change the number of Nginx processes in the Nginx configuration document NGINX.CONF, according to this question # of Nginx worker processes, recommended Nginx Worker_pro The cesses process number is set to 1 because:


But our goal is to allocate different worker_processes processes to the CPUs on different cores, so with my current VPS provider Linode's 512 configuration for example, the server hen has 8 logical CPUs, but as a user I have only 4 logical CPUs, So here I set up to run 3 worker_processes processes because, by default, other programs run on the 1th logical CPU (CPU0), in order not to affect other programs, I will allocate these 3 processes to another 3 logical CPUs (CPU1, CPU2, CPU3) run on:

Worker_processes 3;

In general, with a few logical CPUs, it is set to a few worker_processes appropriate, but worker_processes more than 8 will not make much sense, the reason for the above paragraph in English has been mentioned.

You can use this command to view the current CPU situation:

Cat/proc/cpuinfo

Or show the number of logical CPUs directly:

Cat/proc/cpuinfo |grep "Processor" |wc-l

Nginx worker_cpu_affinity Settings

According to the information on the Nginx Wiki:

Worker_cpu_affinity default is not open, according to the example we can see that 0001 0010 0100 1000 for 1th, 2, 3, 4 logical CPUs, so we can set 0010 0100 1000来 to bind 3 processes to 2nd, 3, respectively. , 4 logical CPUs:

Worker_processes 3;
Worker_cpu_affinity 0010 0100 1000;

Also, as we can see from the example, worker_cpu_affinity can bind the same 1 processes to 2 logical CPUs:

Worker_processes 2;
Worker_cpu_affinity 0101 1010;

0101 is the 1th, 3 logical CPUs, 1010 is 2nd, 4 logical CPUs.
Nginx processes to allocate up to-core CPU settings enabling and testing

Save the Nginx.conf file and reload the Nginx configuration file:

/usr/local/nginx/sbin/nginx-s Reload

Run AB on another host with Apache installed to test:
Ab-n 20000-k http://www.1990y.com/p.php
You can test a PHP page that requires a MySQL query, or you can test a static picture or page.

You can use top to view the current server situation on the tested server, and then click to display the usage of each logical CPU.

Turn on CPU multi-core utilization

Rule setting
(1) The number of cores in the CPU, there are several numbers, 1 on behalf of the kernel open, 0 for the kernel shutdown
(2) worker_processes up to 8, 8 more performance will not be upgraded, and stability will be lower, so 8 processes enough

Demo Example
two nuclear CPU, open two processes

  Worker_processes 2; 
  worker_cpu_affinity 01 10; 

01 indicates that the first CPU kernel is enabled, and 10 indicates that the second CPU kernel is enabled
Worker_cpu_affinity 01 10 Indicates that two processes are opened, the first process corresponds to the first CPU kernel, and the second process corresponds to the second CPU kernel

Two-core CPU, open eight processes

  Worker_processes 8; 
  worker_cpu_affinity 01 10 01 10 01 10 01 10; 

8 processes have been opened, each of which corresponds to the opening of 2 cores

8 Nuclear CPU, open 8 processes

  Worker_processes 8; 
  Worker_cpu_affinity 10000000 01000000 00100000 00010000 00001000 00000100 00000010 00000001; 

00000001 means to open the first CPU kernel, 00000010 to open the second CPU kernel, and so on

8 Nuclear CPU, open 2 processes

  Worker_processes 2; 
  Worker_cpu_affinity 10101010 01010101; 

10101010 indicates that the 2,4,6,8 kernel is turned on, and 01010101 indicates that the 1,3,5,7 kernel is started
2 processes correspond to 8 cores

Restart Nginx
The Nginx service needs to be restarted after the configuration is complete

  /etc/init.d/nginx restart 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.