How to Optimize Nginx for High Traffic?

Nginx was developed in 2004 by Igor Sysoev. Nginx was developed to fix the C10K problem. This error has to do with handling 10K concurrent connections.

How-to-optimize-nginx-for-high-traffic
How-to-optimize-nginx-for-high-traffic

Even though Nginx is powerful and can handle a good amount of traffic. Still, there are methods to optimize Nginx for high traffic so it can handle way more traffic than usual.

Prerequisites

  • Linux Server with root or sudo user

Tuning Nginx’x Worker Processes

In Nginx, the worker process handles every request that lands on the server. By default, the worker process is set to “auto” This can be adjusted as per the number of cores available on your server so that each core is independent of their work.

If you are unsure about the number of cores your server has, you can execute the following command

$ sudo grep processor /proc/cpuinfo | wc -l

In our case, the output was “4”.

so open “Nginx.conf” file

$ sudo nano /etc/nginx/nginx.conf

Change

worker_processes auto;

to

worker_processes 4;

Updating the number of worker connections

Worker connections are the total number of connections worker processes can handle simultaneously. By default, it is set to 512 and you can modify it.

But before that, you must know the limit you can exceed. you can check the limit by using

$ sudo ulimit -n

In our case, it was “1024”

so open nginx.conf

$ sudo nano /etc/nginx/nginx.conf

Change

worker_connections 512;

to

worker_connections 1024;

Adjusting buffer size

When a connection between a server and a client is established, a buffer holds a part of the response until it is filled. But if the response size is bigger than the buffer size, Nginx will write the response to disk which means the performance of the server decreases. To recover this, we can adjust the buffer size as per our requirements

You can copy and paste the below directives to your http { block in Nginx.conf file

client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 2 1k;

Disabling Logs

Nginx keeps a log of each and everything. This consumes data requests as it affects I/O cycles which is enough to cause CPU performance issues. So we recommend you disable the logs

Open nginx.conf

Copy and paste the below directive under http { block

access_log off;

File Cache Opening

In Linux, everything is a file and when we use directives like open_file and file descriptors, all files will be cached to the server. Serving static files like CSS, HTML, and JS with open_file cache will boost the performance of NGINX because the files are stored in the cache for a limited time.

To configure this, copy and paste the below code under http { block

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

Enabling static file cache

Usually, CSS and JS files are static and they don’t change as much as HTML does. So enabling the cache of these static files will boost the performance.

Open your vhost.conf file and copy and paste the below code under nginx server block

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

These changes will ensure the good performance of Nginx.

Category:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *