Website gzip compression makes it possible to reduce the file size of a web file (like HTML, PHP, CSS and Javascript files) to about 30% or less of its original size before these files get sent to the browser of a user. This compressed file is then served to the browser of the user which in turn decompresses it automatically to load the full original file in the browser again. Enabling gzip compression is great for improving page speed because your visitors will need to download much smaller web files as the original ones when browsing your web pages , which speeds up the download process of these files. There’s no reason to not use it these days.

Enable gzip compression using the .htaccess file for Apache

You can actually use two different Apache mods to enable HTTP gzip compression: mod_gzip and mod_deflate. Mod_gzip enables gzip compression and mod_deflate makes it possible to compress the output from your server before it is being sent to your visitor (which is the same thing). So should you be compressing your resources with gzip or deflate? In the end it doesn’t matter much, both modules will provide you with the same maximum gzip compression possible. But, as a general rule it is recommended to use mod_deflate since it’s more widely supported as mod_gzip. Mod_deflate is also better documented and easier to configure. If mod_deflate doesn’t work on your server you can still use mod_gzip. Not every host has these modules enabled on their servers, so make sure you ask your host about this when the below .htaccess scripts do not work. Add one of the below scripts to your .htaccess file (which can be found or should be placed in the root folder of your website (usually /var/www/html)):

To enable mod_deflate (recommended):

<IfModule mod_deflate.c>
  # HTML, CSS, JavaScript, Text, XML und fonts
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/opentype
  # For Olders Browsers Which Can't Handle Compression
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>

To enable mod_gzip (second back up choice):

<ifModule mod_gzip.c>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_include mime ^text/.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
  mod_gzip_item_exclude mime ^image/.*
  mod_gzip_item_include handler ^cgi-script$
</ifModule>

A note:

When you just want to compress certain components or a certain file type with gzip make sure you include the file type to one of the above lists. And again, make sure at least one of the mods are enabled on the server where your website is hosted. If you don’t want to compress a certain file type, just remove the associated file type from the code.

Enable gzip compression for nginx

Edit your nginx.conf file or create a new config file called /etc/nginx/conf.d/static_gzip.conf:

$ sudo vi /etc/nginx/nginx.conf

Add the following in http context:

#
# 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_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# Specify the minimum length of the response to compress (default 20)
gzip_min_length 500;

Save and close the file. Verify that there are no errors in config file:

$ sudo nginx -t

Restart the nginx server

$ sudo systemctl restart nginx

Gzip on Windows Servers (IIS Manager)

When your site is hosted on a Windows server you can enable compression for both static and dynamic files. It’s fairly easy to set this up in the IIS manager:

  1. Open up IIS Manager
  2. Click on the site you want to enable compression for
  3. Click on Compression (under IIS)
  4. Now Enable static compression and you are done!
    When you can’t get it working visit Microsoft’s guide on this subject to learn how you can set it up.

Reference:
https://www.giftofspeed.com/enable-gzip-compression/