In this tutorial, we will show you how to enable Gzip Compression on Nginx. For those of you who didn’t know, Nginx is one of the most popular web servers in the world and is responsible for hosting some of the largest and highest-traffic sites on the internet. It is more resource-friendly than Apache in most cases and can be used as a web server or a reverse proxy. So today I’m going to show you how to set up enable Gzip compression on Nginx on CentOS 6 or 7. Compressing your scripts and images is a good idea to optimize your website’s load times.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step enable Gzip Compression on Nginx.
Prerequisites
- A server running one of the following operating systems: Ubuntu or CentOS based.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Enable Gzip Compression on Nginx
Step 1. Configure Nginx Configuration.
Now we edit nginx.conf
(/etc/nginx/nginx.conf
) :
nano /etc/nginx/nginx.conf
Add the following lines:
## enables GZIP compression ##gzip on;## compression level (1-9) #### 4 is a good compromise between CPU usage and file size. ##gzip_comp_level 4;## minimum file size limit in bytes, to low can have negative impact. ##gzip_min_length 1000;## compress data for clients connecting via proxies ##gzip_proxied any;## disables GZIP compression for ancient browsers that don't support it. ##gzip_disable "msie6";## compress outputs labeled with the following MIME-types. #### do not add text/html as this is enabled by default. ##gzip_types application/json application/javascript application/xml text/css text/javascript text/plain text/xml
;
Next, save and close the file, then restart your Nginx service:
sudo systemctl restart nginx
If you wish to test if GZIP is enabled, use this command:
curl -H "Accept-Encoding: gzip" -I http://idroot.us
With that file now in place, restart your server and you will now be serving site assets with gzip compression. Google takes site speed into account when ranking and placing your sites in their search engine so do your users a favor and strive for the fastest site possible, especially for mobile users.
Congratulations! You have successfully enabled Gzip on Nginx. Thanks for using this tutorial for enabling gzip compression Nginx on the Linux system. For additional help or useful information, we recommend you check the official Nginx website.