Enable Nginx Gzip Compression – Ubuntu 18.04 & 16.04 – Nginx, stylized as NGINX or nginx or NginX, is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. The software was created by Igor Sysoev and publicly released in 2004. Nginx is free and open-source software, released under the terms of the 2-clause BSD license.
What are the 10 advantages of Nginx?
- Installations and configurations are simple and easy
- Fastest and the best for serving static files
- Dynamic content transformed into static content
- When compared to Apache, 4 times more concurrent connections are handled.
- Compatibility with commonly-used web apps
- Load Balancing Support
- Nginx makes websites faster thereby enabling them to get higher ranking by Google
- No risk in switching over to Nginx
- Support from Nginx service professionals
- The unique opportunity to try something new
Why is it important?
The main reason it is important is because it reduces the time it takes for a website to transfer the page files and style sheets which ultimately reduces the load time of your website.
What is Gzip compression?
When a user hits your website a call is made to your server to deliver the requested files.
The bigger these files are the longer it’s going to take for them to get to your browser and appear on the screen.
Gzip/Brotli compresses your webpages and style sheets before sending them over to the browser. This drastically reduces transfer time since the files are much smaller.
In terms of cost versus benefit, compression should be near the top of your page speed optimizations if you don’t have it setup already.
How does it work?
Gzip/Brotli is actually a fairly simple idea that is extremely powerful when put to good use. Gzip/Brotli locates similar strings within a text file and replaces those strings temporarily to make the overall file size smaller.
The reason gzip/Brotli works so well in a web environment is because CSS files and HTML files use a lot of repeated text and have loads of whitespace. Since gzip/Brotli compresses common strings, this can reduce the size of pages and style sheets by up to 70%!
Gzip/Brotli has to be enabled on your webserver which is relatively straight forward.
When a browser visits a webserver it checks to see if the server has gzip/Brotli enabled and requests the webpage. If it’s enabled it receives the gzip/Brotli file which is significantly smaller and if it isn’t, it still receives the page, only the uncompressed version which is much larger.
Enable Nginx Gzip Compression on Nginx?
Configuring Nginx’s gzip Settings
To change the Nginx
gzip configuration, open the main Nginx configuration file in
nano or your favorite text editor.
$ sudo nano /etc/nginx/nginx.conf
gzip settings section, which looks like this:
You can see that by default,
gzip compression is enabled by the
gzip on directive, but several additional settings are commented out with
# comment sign. We’ll make several changes to this section
Once you done please reload Nginx to take effect changes
$ sudo systemctl reload nginx