Image

Knowledge base → Optimizing the speed of request processing by the nginx service

[Virtual servers] [Applications on VPS/VDS]
Date of publication: 26.03.2024

Optimal nginx settings allow you to increase the speed of request processing. On shared hosting, as a rule, all settings have already been made by the hosting provider’s specialists. And in the case of hosting the site on a virtual server, all settings are made independently.

To make the site work faster, we recommend checking for a configuration that will allow the nginx web server to work faster.

1. Let’s check the basic settings

nano /etc/nginx/nginx.conf

1.1 The option automatically determines the number of cores on the virtual server.

worker_processes auto;

1.2 Compression settings

         gzip on;
         gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/ xml+rss text/javascript;

1.3 Let's enable http/2

Let's add http2 to the server section of the web host file after ssl in the listen line. This option should only be added for https.

server {
     listen 89.xx.77.xx:443 ssl http2;
     listen [2a01:xx:4:x::xx]:443 ssl http2;

1.3.1 Let's check the operation of HTTP2

nginx -t
service nginx restart
curl -I https://your-domain.tld

We get the following output:

HTTP/2 200
server: nginx
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
cache-control: no-cache, private
date: Tue, 26 Mar 2024 10:39:35 GMT

1.4 Let's configure the maximum number of connections

The default is 768, let's change the number to the one specified in the operating system.

1.4.1 Let's check the value

ulimit -n

1024

1.4.2 Set it in the nginx settings file

nano /etc/nginx/nginx.conf
events {
worker_connections 1024;
}

1.5 Adjust the buffer size

By default, nginx has a buffer size of one page of memory. In order for it to be able to process more requests from the cache, we will change the values inside the http {} section of your host.

nano /etc/nginx/conf.d/domain-tld.conf
http {
...

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

...
}

1.6 Set up the timeout for request processing

Under high load and low waiting time, the client may receive an error message when the nginx web server has not received a response from php-fpm, in this case the waiting time should be increased.

Please note that increasing the waiting time can have a negative impact in case of flooding or ddos attacks, as this will require more resources from the server.

nano /etc/nginx/conf.d/domain-tld.conf
http {
...

client_body_timeout 12;
client_header_timeout 12;
keepalive_timeout 15;
send_timeout 10;

...
}

Now your nginx will process requests with greater speed. After making all changes, check the configuration file for errors and restart the service to apply the changes.

nginx -t
service nginx restart

Ready.





No Comments Yet