Tutorials > Filter and optimize static file requests with NGINX on Ubuntu 18.04

Filter and optimize static file requests with NGINX on Ubuntu 18.04

Published on: 16 January 2020

Cache NGINX Optimization

NGINX is a high-performance web server used especially to manage static files and as a reverse proxy, but that, thanks to its versatility, can be exploited in any type of situation.

In this guide you will learn how to filter and optimize static file requests with NGINX, in order to increase your website performance and reduce the server load, using some tricks, such as enabling Gzip compression, changing the number of processes / connections or the size of the buffers.

First, connect to your server via an SSH connection. If you haven’t done so yet, following our guide is recommended to connect securely with SSH. In case of a local server, go to the next step and open the terminal of your server.

Modification of the Configuration Parameters

Open the NGINX configuration file using an editor, such as nano or vi. For this example, nano was used.

$ sudo nano /etc/nginx/nginx.conf

Enabling Gzip compression

By enabling Gzip compression, the amount of data to be transmitted can be reduced. Be careful not to increase compression level of Gzip too much, because it may affect the CPU performance.

gzip             on;

gzip_comp_level  2;

gzip_min_length  1000;

gzip_proxied     expired no-cache no-store private auth;

gzip_types       text/plain application/x-javascript text/xml text/css application/xml;

Setting the number of processes and connections

The parameter ‘worker_processes’ refers to the number of processes instantiated by NGINX after starting it. The number of processes should normally be equal to the number of cores on the server. For example, if the server has a processor with 4 cores the parameter with the value 4 has to be set, as follows:

worker_processes 4;

The parameter worker_connections refers to the number of clients that will be served by a single process. To set an optimal value for the configuration, all you have to do is check the limits of your cores with the command:

$ ulimit -n

The result will be the maximum number of connections per process. Then, to set it:

worker_processes 1024;

Changing the size of the Buffers

If the size of the buffers is too low, NGINX will be forced to create temporary files, causing numerous disk operations. To avoid it, set the size of the buffers correctly.

client_max_body_size: indicates the maximum size of the client request

client_body_buffer_size: refers to the buffer size for POST requests

client_header_buffer_size: refers to the buffer size relative to the client request header

By modifying these parameters with the following values, a good result should be obtained:

client_max_body_size 8m;

client_body_buffer_size 10K;

client_header_buffer_size 1k;

Changing the duration of the cache

To manage the duration of the static file cache, all you have to do is specify the file extensions and their duration in this way:

location ~*.(jpg|jpeg|png|gif|ico|css|js)$ {

expires 365d;

}

where with 365d it is specified that the cache for its extensions will last 365 days.

Reducing Timeouts

The client_body_timeout and client_header_timeout parameters respectively indicate the waiting time for the body or header of the request. If the server does not receive the body or header within this time, \ the connection is ended with a timeout error.

The send_timeout parameter indicates the maximum time between two read operations. if this time is exceeded, NGINX will end the connection with the client.

Instead, the keepalive_timeout parameter refers to the maximum duration of a Keep-Alive connection before terminating it.

By setting these values ​​as follows, a good result should be obtained:

client_body_timeout 12;

client_header_timeout 12;

send_timeout 10;

keepalive_timeout 15;

Disabling Logs

NGINX monitors every request made in a log file. To disable this function, all you have to do is change the access_log parameter as follows:

access_log off;

Applying changes

Restart the NGINX service to implement the changes:

$ sudo service nginx restart