Reading Time: 5.30 mins

What is Nginx?

Nginx, an open-source web server which is popularly known for its simple configuration, stability, utilization of the low resource and high performance. Being compelled by an event-driven and asynchronous architecture, it can manage thousands of concurrent connections and owns the capacity to turn on 50% of the traffic sites.

Still not installed the Nginx server on your system? To know the installation procedure of the Nginx server, read Installing Nginx on Ubuntu 18.04

Optimizing the Nginx Server’s Performance:

Apart from being the HTTP server efficiency, it can also function as a reverse proxy, proxy server (for mail), load balancer and HTTP cache server. By following an event-driven approach, Nginx can handle multiple-requests within a single processing thread. Thus, optimizing the web server like Nginx to achieve an effective performance is not a simple thing and needs effective implementation too.

To achieve this effective performance, follow the optimization of the below parameters in the file,

sudo nano /etc/nginx/nginx.conf

Worker Processes

Nginx generally contains one master process and numerous worker processes to handle the requests. It is the worker processes which involved in the task of request processing. As far as the number of worker processes is concerned, it can be identified from the configuration file. The number is either fixed as per the given configuration or automatically adjusted according to the available cores of CPU.

This directive determines the number of processes to spawn in the Nginx soon after the server is connected to the ports. It is recommended to set one worker process per CPUs core. Raising the processes above the CPU’s core will slow down the performance of your system.

The default value of the worker processes is generally set to auto.

To know the number of CPU cores in your system, use the command:

grep processor /proc/cpuinfo |wc -1

As per the above info, the server has one core CPU, hence it is best to set value one for the worker process as:

worker_processes 1;

If you need to run more worker processes than the CPU cores, it is recommended to upgrade your system with more CPU cores before adjusting the worker processes to match the cores.

Worker Connections

This determines the number of clients/requests to be served concurrently by the Nginx server. The default value of the worker connections is set to 768 by default. It is necessary to unleash the maximum potential of the server via required adjustment as every browser usually opens at least two connections per second (to consider half the number).

To calculate the max. no. of clients that Nginx can serve:

worker_processes *worker_connections = max. no. of client

Use the ulimit command to check the core’s limitations as follows,

ulimit -n

Let us assume the number 1024, (reading of smaller machine – 512MB droplets)

Now, update the values in the configuration file by using the command:

sudo nano /etc/nginx/nginx.conf
worker_processes 1;
worker_connections 1024;

Applying the calculation,

worker_processes*worker_connections  = max. no. of clients
1*1024 = 1024

From the above case, it is found that the 1024 number of clients can be served per second by the Nginx server.

Gzip Compression

The Gzip compression helps to reduce the size of the client’s response and thereby results in the consumption of lesser bandwidth and better page load time. At the same time, increasing the compression also leads to the wastage of CPU cycles.

Following is a sample configuration of the gzip compression:

gzip on;
gzip_comp_level 2;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml   application/xml+rss text/javascript;


When it comes to optimization of buffers, there are four main directives to consider, they are


This determines the size of the post-action sent to Nginx. The recommended value to be set in 10K.


This determines the size of the buffer which the Nginx server assigns to request headers. The recommended value to be set is 1K. Sometimes, the headers may also contain lengthy URLs and chunks of cookie data.


Limiting the upload size of a file comes in great support in avoiding the denial-of-service (DOS) attacks and other related conflicts. You can use the client_max_body_size directive (a part of ngx_http_core_module ) to set the client body size.

Nginx’ default client_max_body_size on file uploads is 1MB.


This determines the number and size of larger_client_header_buffers which assists in storing the client requests at the time when the default event buffer is inadequate.


Optimizing the following variables helps to enhance the performance of the Nginx server to a greater extent.


The waiting time of the Nginx server for the client body to be sent after a request. If the client_body is not sent, then the server will end up showing 408 Error (or Request Time Out).


The waiting time of the Nginx server for the client header to be sent after a request. If the client_header was not sent, then the server will end up showing 408 Error (or Request Time Out).


It is the time taken for the keep alive connection with the clients, post which the server terminates the connection.


It is the time taken for sending the response to the concerned client. Upon failure of this process, the Nginx server terminates the connection.

client_body_timeout = 12
client_header_timeout = 12
keepalive_timeout = 15
send_timeout = 10