In this tutorial, you’ll learn how to enable gzip compression on your Nginx server. This works on any website (including WordPress) that runs on Nginx.
Why Enable Gzip Compression on Nginx?
For those of you who don’t know, gzip compression allows your website to load faster.
On a good day, if your web page is 4 MB, gzip compression will reduced the size of your web page to 2 MB. This can improve page load times by a significant amount.
Note that the client web browser must perform an additional decompression step before rendering the web page, but most times the network time savings outweigh the overhead.
A lot of times, when running a test on PageSpeed Insights, Google suggests to “Enable text compression”. Similarly, they explain:
Text-based resources should be served with compression to minimize total network bytes.
By enabling Gzip compression, you can fix this PageSpeed Insights error on WordPress and non-WordPress websites alike.
Enable Gzip Compression in Nginx
To enable Gzip compression in Nginx, you will need root access to your server. On your server you want to edit the
/etc/nginx/nginx.conf file and add the following.
Please note that while probably all modern browsers support gzip compression, the gzip_disable line disables gzip compression for certain versions of Microsoft Internet Explorer.
After making changes, restart your Nginx server with the
systemctl restart nginx command.
Verify It’s Working
You can verify that gzip compression is working with Chrome DevTools. In Google Chrome, go to View -> Developer -> Developer Tools. Open the Network tab and load a page from your website.
If you have any questions about gzip on Nginx, let me know in the comments below.