Optimizing Nginx For Performance Pt1 : Configure Worker Processes for NGINX

NGINX Tweaks and Performance Tips

 What is Nginx:

As stated on the Nginx Wikipedia Page, “Nginx (pronounced “engine-x”) is an open source reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer, HTTP cache, and a web server (origin server).” Nginx is quickly becoming one of the most popular web servers available today. Nginx’s accelerated adoption rate is due to the speed benefits that it offers over competitors like Apache. In fact, most of the top rated and popular websites incorporate Nginx into their configuration. Nginx can be used as a stand alone web server or incorporated into a more complex setup accompanying other servers etc. If you’re new to Nginx, you’ll probably be surprised at how useful it can be.

Note:  It goes without saying, but please do not implement any of these changes directly into a live production environment. 


Part 1 – Configure Nginx worker_processes:

Work Processes Explained: Nginx has one master process and several worker processes. The main purpose of the master process is to read and evaluate the configuration, and maintain spawned worker processes. The spawned worker processes do the actual handling of requests. The event-based model and OS-dependent mechanisms in Nginx distribute requests evenly among spawned worker processes. Nginx makes it possible to specifically define the number of work processes that should be spawned. Adjusting this number to reflect your system configuration (CPU Cores) offers great performance benefits.

How to make this adjustment in the nginx.conf:

1.) Determine your number of CPU cores/processors.

You can determine your number of processor cores by reading the value from proc/cpuinfo.

Use the below command to find this value

cat /proc/cpuinfo |grep processor |wc -l

2.) Change the worker_processes directive to match this number.

The worker_processes directive in the configuration tells your server how many cores are assigned to each processor.  Most experts suggest adjusting this directive to match your CPU core count 1:1.  For example:  if your server has 2 cores, you should set the directive to the reflect the value 2.  The default Nginx configuration file  is located at: /etc/nginx/nginx.conf

Use your favorite editor (vi, vim, nano etc) to edit this file.

Find the ‘worker_processes’ directive and change the value that directly follows.

When you’re finished, the line should be similar to below:

worker_processes 2;

3.) Take a step further by editing the worker_connections directive. 

This directive sets the maximum number of connections that can be processed at one time by each worker process. By default, the worker connection limit is set at 512, but it’s very likely that your server can handle more than this.

The appropriate value for this directive can be discovered through simple tests, as it is variable based on the type of traffic being handled. Your server’s core limitations can also be discovered by using the following command:

[scott@webserver ~]# ulimit -n

The returned output will be a number value.

You can also set the use epoll directive (Explained: this is  a scalable I/O event notification mechanism that will trigger on events and ensure that server I/O is utilized efficiently).

Also, you can utilize the multi_accept directive in order for a worker to accept all new connections simulataneously.

Example of the completed events directive:

events {
	worker_connections 1023;
	use epoll;
	multi_accept on;

4.) Save the configuration file and restart your Nginx Server.




Related Posts

Write a comment