Nginx is a fast, open-source, and reliable web server. More than 40% of websites use Nginx because it needs fewer resources, high scalability, easy configuration, and support for various protocols. Still, there are some tuning tips that you can use to boost Nginx performance.
Following the mentioned changes, your website’s performance will increase by 50%.
Prerequisites
- Working server with LEMP installed and configured. You can follow our initial setup guide for Ubuntu or initial setup guide for Rocky or Alma Linux here.
- Root or sudo user
Enabling HTTP2
When you open your virtual host file, you will find some directives as
listen [::]:443 ssl ipv6only=on;
listen 443 ssl;
Add “http2” as a suffix as shown below
listen [::]:443 ssl http2 ipv6only=on;
listen 443 ssl http2;
Press “Ctrl+x“, “y” and hit “Enter” to save the file
It’s done. You have successfully enabled “Http2” to your webserver.
SSL Caching and optimization
Almost every website today uses SSL. It’s the most basic thing that we add while configuring the web server. Whenever a user visits your website, an SSL handshake takes place between the browser and the server. Initial handshaking is the most taxing part, to avoid that we use SSL caching that will improve the overall performance of a website.
Open nginx.conf file
$ sudo nano /etc/nginx/nginx.conf
Copy and paste the below-mentioned code under http { block
# SSL
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
Press “Ctrl+x” “y” and hit “Enter” to save the file
Enabling Gzip
The website’s performance depends a lot on the size of the files. The bigger the size of the file the longer it will take to download and open. With Gzip, we will compress all the files that will move to the user’s browser. Gzip is a module that comes installed by default with Nginx and we have to enable it by making some changes
Open nginx.conf file
$ sudo nano /etc/nginx/nginx.conf
Find the gzip directive and make changes as mentioned below
##
# `gzip` Settings
#
#
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types
application/atom+xml
application/geo+json
application/javascript
application/x-javascript
application/json
application/ld+json
application/manifest+json
application/rdf+xml
application/rss+xml
application/xhtml+xml
application/xml
font/eot
font/otf
font/ttf
image/svg+xml
text/css
text/javascript
text/plain
text/xml;
Press “Ctrl+x” “y” and hit “Enter” to save the file
It’s done. Gzip if enabled in your website.