How to Speed up Your Nginx Website

How to Speed up Your Nginx Website

In this tutorial, we will explain how to tune and optimize your Nginx configuration file to help speed up your website’s performance.

Nginx is a fast, lightweight, and high-performance web server in charge of serving at least 40% of all websites globally. When compared to other web servers, Nginx is primarily known for its speed, with it capable of handling thousands of concurrent connections with ease. Nginx can also be used as a load balancer, a reverse proxy, and an HTTP caching server. It uses little resources and is ideal for delivering static content.

To help further improve performance, Nginx has a built-in caching system that allows dynamic requests to be cached directly on the server for future requests, thereby reducing the load on your CPU and system in general.

Enable Gzip Compression

If you want to increase your website speed, Gzip compression is considered a high-priority recommendation by site speed test tools.

Gzip compression used to reduce the size of files request by client’s web browser. This will reduce the amount of time required to transfer a resource from the server to a browser. It utilizes less network bandwidth and improves the page load time for slow connections, while also reducing your file size by up to 70%.

We also recommended to configure NGINX so that it only compresses large files and avoids smaller files. You can enable the Gzip compression by editing the Nginx virtual host configuration file:

nano /etc/nginx/sites-enabled/your-domain.conf

Add the following lines within “http {” section:

gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_min_length 1024;
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;

Save the file then restart the Nginx service to apply the changes.

systemctl restart nginx

Next, you can verify the Gzip compression with the following command:

curl -I -H 'Accept-Encoding: gzip' http://your-domain.com/

If Gzip compression is enabled, you should see the “Content-Encoding: gzip” in the following output:

HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Tue, 01 Sep 2020 07:54:04 GMT
Content-Type: text/html
Last-Modified: Tue, 04 Mar 2014 11:46:45 GMT
Connection: keep-alive
Content-Encoding: gzip

Enable HTTP/2 Support

By default, Nginx is configured to use HTTP/1 protocol. Compared to HTTP/1, HTTP/2 has a lot of benefits as it allows a web browser to download files in parallel, and allowing the server to push resources, among others. Currently, HTTP/2 has gained the popularity due to its performance features. Enabling this can help speed up your Nginx website.

Features

  • Binary Nature
  • Multiplexing
  • Uses header compression
  • Uses one connection for parallelism
  • Implements server push

Before enabling HTTP/2 on Nginx, it is necessary to enable HTTPS on your server because almost all browsers allow HTTP/2 only over HTTPS.

You can enable HTTP/2 by editing the Nginx virtual host configuration file:

nano /etc/nginx/sites-enabled/your-domain.conf

Find the following line:

listen 443 ssl;

And, replace it with the following line:

listen 443 ssl http2;

Save and close the file then restart the Nginx service to apply the changes.

systemctl restart nginx

Next, verify the HTTP/2 protocol with the following command:

curl -I https://your-domain.com

Running that should give you this kind of output:

HTTP/2 200
server: nginx
date: Tue, 01 Sep 2020 08:19:13 GMT
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
x-powered-by: your-domain.com

Configure Worker Processes to Improve Speed

A worker process is a single-threaded process. Nginx has a one or more worker processes and each capable of processing a large number of simultaneous connections. By default, the worker process value is set to auto in Nginx default configuration. It is recommended to set 1 worker process per CPU core. Adding more worker processes will almost always improve the speed of your Nginx server.

You can configure the Worker process by editing your Nginx main configuration file:

nano /etc/nginx/nginx.conf

Find the following line:

worker_processes auto;

And, replace it with the following line as per your CPU cores:

worker_processes 1;

Save and close the file then restart the Nginx service to apply the changes.

Configure Worker Connections

Worker connections define the maximum number of connections that each worker process can handle simultaneously. By default, this value is set to 768. It is recommended to set this value equivalent to the number of open file descriptors. You can find the number of open file descriptors using the following command:

ulimit -n

You should get this as your output:

1024

This value can then be set in your configuration by editing the main Nginx configuration file:

nano /etc/nginx/nginx.conf

Change the following lines:

events {
worker_connections 1024;
}

Save and close the file then restart the Nginx service to apply the changes.

Configure Buffers Size

Buffers play a very important role to increase the Nginx web server performance. With buffers, Nginx receives a response from the proxied server as soon as possible, saving it into the buffers set by the proxy_buffer_size and proxy_buffers directives. By default, the buffer size is equal to one memory page.

You can set buffering by editing the Nginx virtual host configuration file:

nano /etc/nginx/sites-enabled/your-domain.conf

Add the following lines within “http {” server block:

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

Save and close the file then restart the Nginx service to apply the changes.

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS

Configure Timeouts

Timeout directives are used for the time a server will wait for a client body or client header to be sent after a request. If the delay is reached, Nginx returns a 408 Request timeout HTTP error. This frees up your server from handling connections for extended periods of time.

You can set it by editing the Nginx virtual host configuration file:

nano /etc/nginx/sites-enabled/your-domain.conf

Add the following lines within “http {” server block:

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

Save and close the file then restart the Nginx service to apply the changes.

Conclusion

In the above guide, you learned different Nginx configuration tips and tricks to speed up the Nginx website. You can now use these settings in the test server, monitor the server speed and tweak the setting with a different value.


Using our managed Nginx hosting provides you with many benefits, such as full server maintenance and management, 24/7 support to help you fix any problems you have, and more, while still giving you complete access and control over everything.

If this tutorial helped you speed up your Nginx website or server, feel free to leave a comment telling us how it helped, or if you have any questions, leave those down in our comment section as well. Thank you.

2 thoughts on “How to Speed up Your Nginx Website”

Leave a Comment