Optimizing Nginx: How to Set worker_processes and CPU Affinity for Best Performance

This guide explains how to edit the nginx.conf file to configure the number of worker processes, determine appropriate values based on CPU cores and workload, and bind workers to specific CPU cores using worker_cpu_affinity, including practical command examples and considerations for blocking I/O.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Optimizing Nginx: How to Set worker_processes and CPU Affinity for Best Performance

All require modifying the nginx configuration file.

vi /nginx/conf/nginx.conf

1. worker_processes – maximum connections per worker

The default worker process count is 1, and each worker can handle up to 1024 connections.

Each worker process is single‑threaded and invokes various modules to provide functionality.

If the modules are non‑blocking, the number of workers should match the number of CPU cores.

If blocking calls may occur, configure a slightly higher number of workers.

For example, if the service reads many static files from disk and memory is limited, disk I/O can block workers briefly, reducing overall performance.

Typically set the number of worker processes to the number of CPU cores or cores × 2.

If you are unsure of the CPU core count, use the top command and press 1 to view each core.

Practical steps

# Edit nginx configuration file
vi /usr/local/nginx/conf/nginx.conf
worker_processes 4;
# Save and exit
# Reload nginx configuration
/usr/local/nginx/sbin/nginx -s reload
# Check process status
ps -aux | grep nginx | grep -v grep

2. worker_cpu_affinity – binding workers to CPU cores

If each worker is heavily loaded, having multiple workers compete for the same CPU can cause synchronization issues.

Conversely, assigning each worker its own CPU enables full concurrency under the kernel scheduler.

Note: worker_cpu_affinity is only effective on Linux systems.

# Example for 2 CPU cores
worker_processes 2;
worker_cpu_affinity 10 01;

# Example for 4 CPU cores
worker_processes 4;
worker_cpu_affinity 1000 0100 0010 0001;

# Example for 8 CPU cores
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

Link: https://blog.csdn.net/u010922732/article/details/90404096

(Copyright belongs to the original author, please delete if infringed)

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

NGINXworker_processesServer Configurationbackend optimizationCPU affinity
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.