Gzip compression is a process where data is compressed on the server side and then decompressed by the client (such as a browser). Enabling gzip compression not only saves network bandwidth but also speeds up the loading of applications, although it consumes some CPU resources on the server. The diagram below illustrates a typical HTTP request and response flow, showing how an original 100KB HTML file is reduced to just 10KB after gzip compression.
Before enabling compression:
After enabling compression:
Apache and Nginx are two widely used web servers on Linux cloud servers. The following describes how to enable gzip compression on these two web servers.
Add the following code block to the Apache configuration 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>
For Nginx servers, gzip is enabled by default. This can be confirmed by checking the Nginx configuration file.
##
# 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;
23-02-2024 02:02:07
22-02-2024 03:19:32
22-02-2024 03:16:03
22-02-2024 03:14:03
22-02-2024 03:11:58