Instructions on using the CPU parameter worker_cpu_affinity for nginx Performance Optimization

Source: Internet
Author: User

 Nginx performance optimization CPU parameter worker_cpu_affinity usage instructions

Official Note: http://wiki.nginx.org/nginxhttpmainmodule?worker_cpu_affinit 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 1000;

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.

One server is q9300 with a single CPU and 4 cores, so you can directly use the official configuration: worker_cpu_affinity 0001 0010 0100;
The result is as follows. It seems that each CPU core is average:

Top-21:17:56 up 4 days, 3 users, load average: 0.25, 0.44, 0.47
Task: 238 total, 1 running, 237 sleeping, 0 stopped, 0 zombie
Cpu0: 0.3% us, 0.3% Sy, 0.0% Ni, 94.2% ID, 5.2% wa, 0.0% hi, 0.0% Si, 0.0% St
Cpu1: 0.3% us, 0.0% Sy, 0.0% Ni, 96.2% ID, 0.6% wa, 0.6% hi, 2.2% Si, 0.0% St
Cpu2: 0.3% us, 0.0% Sy, 0.0% Ni, 93.5% ID, 5.5% wa, 0.0% hi, 0.6% Si, 0.0% St
Cpu3: 0.0% us, 0.0% Sy, 0.0% Ni, 96.5% ID, 3.5% wa, 0.0% hi, 0.0% Si, 0.0% St
Mem: 8119372 k total, 3641940 K used, 4477432 K free, 504900 K Buffers
Swap: 8385888 k total, 10664 K used, 8375224 K free, 2407512 K cached

PID user PR Ni virt res shr s % CPU % mem time + command
30743 Apache 20 0 6632 3800 812 s 0.3 0.0. 51 nginx
30745 Apache 20 0 7156 4384 s 812 0.3. 85 nginx


The most important thing is not clear. How to express each CPU?
After several searches in the sky

Then, the 16-core CPU should be allocated to one CPU for each process

The CPU of the VPs on this site is Intel (r) Xeon (r) CPU e5520 @ 2.27 GHz X 2, so it is written:

Worker_processes 2;
Worker_cpu_affinity 01 10;


If you want to enable 4 processes with 2-core CPU, you can write it as follows:

Worker_processes 4;
Worker_cpu_affinity 01 10 01 10;


the dual-core is 01, and the quad-core is 00000001. The number of cores is several digits. 1 indicates that the kernel is enabled, 0 indicates that the kernel is disabled.
worker_processes can be enabled with a maximum of eight worker nodes. Performance Improvement of more than eight worker nodes will not be improved, and stability will be reduced. Therefore, eight worker nodes are sufficient.
restart nginx and run/etc/init. d/nginx restart
test whether nginx is useful to multiple CPU cores. Execute AB .exe-C 1000-N 1000 HTTP on another machine: // your website
AB .exe is a performance testing tool installed with Apache. It can simulate concurrent requests from multiple clients.
run the TOP command on the server and press 1 to view the CPU kernel. If the utilization of multiple CPU cores is similar, nginx has successfully used multi-core CPUs.
after the test, the CPU kernel load should be reduced at the same time.

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.