NGINX is a popular, free, and open-source web server. The default NGINX configurations are good enough to get the web server working.

However, if you want to use NGINX to its fullest, you need to play with its configuration files and set the parameters that will optimize the server’s performance. You will find the configuration files in the /etc/nginx directory on a Linux machine.

What Is NGINX?

NGINX is a modern web server that you can use as a media streamer, mail server, reverse proxy, load balancer, cache server, and more.

Used by many big profiles such as VMware, IBM, Cisco, Apple, Microsoft LinkedIn, Netflix, Facebook, Twitter, etc., NGINX is widely known for its top-notch performance. Another advantage of using NGINX is that it is easy to configure and learn.

Prerequisites to Tune NGINX Performance on Linux

You need to satisfy the following requirements before starting with NGINX configurations:

  1. An NGINX server deployed and configured on Linux
  2. A basic understanding of NGINX and its configuration files

If you meet these requirements, you are ready to tune NGINX for optimum performance. Let’s see how to do it.

1. Configure Worker Processes in NGINX

NGINX architecture consists of one master process and several worker processes. The job of the master process is to assess the configuration and manage workers. On the other hand, a worker process’s role is to deal with incoming requests and create a connection between the client and server.

The process value is set to auto by default. This sets the number of worker processes equal to the number of available CPU cores. To know how many CPU cores are present in your system, run the following command:

        grep processor /proc/cpuinfo | wc -l
    

If you want to increase the number of worker processes, you need to configure this in the NGINX configuration file.

Open the file with nano:

        nano etc/nginx/nginx.conf
    

To configure more worker processes, change the default value to the maximum number of available CPU cores in your system.

nginx.configuration file showing 4 processors configured

2. Configure Worker Connections

Another parameter that you can modify to enhance NGINX's performance is worker connections. This is the maximum number of TCP connections that each worker process can simultaneously handle.

Most systems have a default value of 512 connections, but many modern systems also support a bigger number. You can check how many connections your system supports with:

        ulimit -n
    

The output will be the maximum number of connections supported. You can then modify the worker_connections variable in the NGINX configuration file to improve performance.

3. Allow GZIP Compression in NGINX

NGINX uses GZIP for file compression and decompression. If enabled in the NGINX configuration file, you can save bandwidth and enhance the website’s loading time when the connection is slow.

To allow GZIP compression, add the following lines in the NGINX configuration file:

        server {
gzip on;
gzip_vary on;
gzip_min_length 10240;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
gzip_disable "MSIE [1-6]\.";
}
commented gzip configuration in nginx configuration file

4. Limit Timeout Value in NGINX

Reduced timeout values also play a major role in enhancing NGINX performance. The keepalive connections reduce the processor and network overhead when opening and closing connections.

You can modify the following parameters in the configuration file to limit timeouts:

        http
{
client_body_timeout 12;
client_header_timeout 12;
keepalive_timeout 15;
send_timeout 10;
}

5. Adjust Buffer Size

You can also adjust NGINX buffers to optimize the server performance. If the buffer size is too low, then NGINX will write to a temporary file that causes huge I/O operations to run constantly.

You need to set the following buffer parameters for NGINX to function at its best:

        http
{
client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 4 4k;
}

6. Disable Access Logs or Enable Access Logs Buffering

Logs consume a large amount of disk space and CPU/IO cycles that can affect the server's performance if it logs every request.

You can disable access logs which will save some disk space and CPU processing. To disable access logs, add the following line to the NGINX configuration file:

        access_log off;
    

Logs are important as they help in troubleshooting an issue. Completely disabling logs is not a good practice. In this case, you can enable access logs buffering. This will allow NGINX to buffer a series of logs and write them to the log file together at once instead of applying different log operations on each request.

Add the following line in the NGINX config file to allow access logs buffering:

        access_log /var/log/nginx/access.log main buffer=16k
    

7. Adjust Static Content Caching Period in NGINX

The content on the website that remains the same across pages is known as static content. Caching of this content allows it to be placed in locations that are easily accessible. This mechanism reduces bandwidth usage, allows fast accessibility, and subsequently improves the website’s performance.

When a client requests static content, the server will serve a cached version of the content. Add the following lines in the virtual host file placed in the /etc/nginx/sites-available directory:

        location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 90d;
}

This configuration will cache the files for 90 days since the last browser access time.

8. Enable Open File Cache in NGINX

You can also use open file cache parameters in the NGINX configuration file to enhance its performance. This directive allows the file descriptor and frequently visited files to be cached to the server.

Add the following lines in the http section in the configuration file to enable open file cache:

        http {
open_file_cache max=1024 inactive=10s;
open_file_cache_valid 60s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
}

Bring the Best Out of NGINX by Modifying Its Configuration Files

A good practice to follow while changing configurations is to deal with one setting at a time and test it. If it works, move to the next setting. If not, you can always change the configuration back to the default value.

By modifying the parameters configured in the NGINX configuration files such as nginx.conf and virtual host files, you can hack NGINX to give the best performance.