worker_cpu_affinity

The `worker_cpu_affinity` directive binds NGINX worker processes to specific CPU cores for enhanced performance.

Syntaxworker_cpu_affinity mask | cpus;
Defaultnone
Contextmain
Arguments1+

Description

The worker_cpu_affinity directive is utilized in the main context to specify which CPU cores can execute a worker process. This helps optimize performance by ensuring that a worker process runs on specific cores, thereby avoiding context switching and cache misses that can occur when processes migrate between cores. The directive takes one or more arguments that define the CPU core affinity. It is particularly beneficial in systems with multiple CPU cores, maximizing resource utilization while minimizing latency.

Parameters for this directive are provided in the format of a list of CPUs, which can be specified in various forms including single-core specifications (e.g., '0'), range specifications (e.g., '0-3'), or a combination of both. Each appended value represents the allowable CPU cores that the corresponding worker can use. When defining multiple workers, the CPU cores can be spread across the workers, further enhancing parallel processing. This configuration helps maintain performance during high-load scenarios and can lead to more predictable response times.

Implementing worker_cpu_affinity may involve some experimentation to determine the optimal configuration based on the specific workload and hardware architecture. Careful observation of system performance should guide these decisions, as the effectiveness of core affinity can vary based on factors like workload patterns and the number of available CPU cores.

Config Example

worker_cpu_affinity auto;  
# Example with specific core assignment for 4 workers:  
worker_cpu_affinity  0001 0010 0100 1000;

Specifying too many CPU cores can lead to performance degradation due to improper load distribution.

Ensure the number of specified CPUs does not exceed the available logical cores on the system.

Incorrect formatting of the CPU mask may lead to NGINX startup errors.

← Back to all directives