Table of Contents
Was this article helpful?
Try Vultr Today with

$50 Free on Us!

Want to contribute?

You could earn up to $600 by adding new articles!

Configure Gzip Compression on Apache and Nginx

Last Updated: Wed, Nov 17, 2021
Scaling Web Servers


Compressing content before sending it over the Internet reduces the size of transmitted data and adjusts the time taken to load the content. Gzip compression makes web applications faster by shrinking web pages, scripts, and stylesheets to smaller sizes before serving them to a visitor on your server.

When a user calls your website through a web browser, the server responds by delivering the requested file. But since bigger files take longer to load, Gzip compression makes the requested files smaller for the browser to decompress before serving them to the user.

In this article, you will enable and configure Gzip compression on Apache and Nginx running on either a Debian or RHEL based server distribution like Ubuntu, CentOS, respectively.


Apache supports Gzip compression through the mod_deflate module referenced by DEFLATE. By default, the module is turned on, but in any case, you should check whether it’s available with the following commands.

On Debian or Ubuntu

$ apache2ctl -t -D DUMP_MODULES | grep deflate

On CentOS

$ httpd -t -D DUMP_MODULES | grep deflate

Your output should be similar to:

deflate_module (shared)

Else, enable DEFLATE

$ sudo a2enmod deflate    

Enable Compression for Apache

Open your Apache virtual host file and enable gzip compression entries line by line. For example, to compress HTML, text, XML, CSS, and Javascript content types, add the following line to your virtual host or .htaccess file.

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript

text/html will compress HTML files, text/plain compresses all text files, text/xml will compress xml files and application/javascript works on Javascript files.

You can also enable compression on specific file extensions by using the SetOutputFilter directive.

<files *.html>
    SetOutputFilter DEFLATE

To enable server-wide compression for all hosted web pages, add the following code to your apache2.conf or httpd.conf.

<IfModule mod_deflate.c>
# Compress HTML
AddOutputFilterByType DEFLATE text/html

# Compress Text Files
AddOutputFilterByType DEFLATE text/plain

# Compress CSS
AddOutputFilterByType DEFLATE text/css

# Compress JavaScript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Compress Images
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon

# Compress Fonts
AddOutputFilterByType DEFLATE application/
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 font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf

# Compress XML Files
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE text/xml



Gzip is enabled on Nginx by default, but in any case, you should open the main configuration file nginx.conf and confirm that the gzip section entries are uncommented.

$ cat /etc/nginx/nginx.conf | grep gzip

# Gzip Settings

gzip on;

# 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;

Enable Compression for Nginx

To turn on Gzip compression, simply add on to the gzip directive in the main Nginx configuration file.

$ sudo nano /etc/nginx/nginx.conf

gzip on;

Add file types to compress.

gzip_types text/plain text/html /text/css application/xml application/javascript;

Set the minimum length of a response to compress, the default is 20 bytes.

gzip_min_length 500;

Save and close the file.

Your Nginx Configuration file should now look similar to the one below.

gzip on;
gzip_types text/plain text/html /text/css text/javascript application/xml application/javascript;    
gzip_min_length 500;

Want to contribute?

You could earn up to $600 by adding new articles