Learn Gzip Compression On Apache And Nginx

December 15, 2019

Table of Contents

    Introduction

    Compressing content before sending it over the wire not only saves bandwidth, it makes web applications faster. The combined time it takes for a web server to compress data and a browser to decompress it, is less than sending just uncompressed data through the wire. This writeup shows you how to configure both Apache and Nginx with gzip compression.

    Apache

    Add below snippet to your apache2.conf or .htaccess file.

    <IfModule mod_deflate.c>
        # compress html, css, javascript, text, xml and fonts
        AddOutputFilterByType DEFLATE application/javascript
        AddOutputFilterByType DEFLATE application/rss+xml
        AddOutputFilterByType DEFLATE application/x-font
        AddOutputFilterByType DEFLATE application/x-font-opentype
        AddOutputFilterByType DEFLATE application/x-font-otf
        AddOutputFilterByType DEFLATE application/x-font-truetype
        AddOutputFilterByType DEFLATE application/x-font-ttf
        AddOutputFilterByType DEFLATE application/x-javascript
        AddOutputFilterByType DEFLATE application/xhtml+xml
        AddOutputFilterByType DEFLATE application/xml
        AddOutputFilterByType DEFLATE font/opentype
        AddOutputFilterByType DEFLATE font/otf
        AddOutputFilterByType DEFLATE font/ttf
        AddOutputFilterByType DEFLATE image/svg+xml
        AddOutputFilterByType DEFLATE image/x-icon
        AddOutputFilterByType DEFLATE text/css
        AddOutputFilterByType DEFLATE text/html
        AddOutputFilterByType DEFLATE text/javascript
        AddOutputFilterByType DEFLATE text/plain
        AddOutputFilterByType DEFLATE text/xml
    
        # you can also compress by file type/extension:
        <files *.html>
            SetOutputFilter DEFLATE
        </files>
    </IfModule>
    

    Nginx

    Gzip should already be enabled on Nginx by default. If not, open its configuration file (sudo nano /etc/nginx/nginx.conf) and make sure that the settings under the gzip section are uncommented (like in snippet 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_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    

    Need help?

    Do you need help setting up this on your own service?
    Please contact us and we’ll provide you the best possible quote!