Dr Clèm's Blog

Tags: Apache HTTP Server

SVG with GZIP Compression in Apache HTTP Server

Friday Oct 20, 2017 16:45, last edition on Friday Oct 20, 2017 17:42

To save bandwidth with Apache HTTP Server, you can compress SVG files. On Ubuntu 16.04.3, Apache HTTP Server comes with version 2.4.27 which enable GZIP Compression by default.

# apache2 -v
Server version: Apache/2.4.27 (Ubuntu)
Server built: 2017-09-28T00:00:00
If not enable, you can do it with mod_deflate
# a2enmod deflate
For SVG Compression, edit the file /etc/apache2/mods-available/deflate.conf and add AddOutputFilterByType DEFLATE image/svg+xml between the IfModule. It should look like this
# cat /etc/apache2/mods-available/deflate.conf
<IfModule mod_deflate.c>
 <IfModule mod_filter.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
  AddOutputFilterByType DEFLATE application/x-javascript application/javascript application/ecmascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE image/svg+xml
 </IfModule>
</IfModule>
Restart Apache HTTP Server
# systemctl restart apache2
Now, let us check if it is working with curl and its -I argument to query only the headers.
% curl -I https://clementfevrier.fr/images/CC-BY-SA_icon.svg
HTTP/1.1 200 OK
Date: Fri, 20 Oct 2017 14:23:11 GMT
Server: Apache/2.4.27 (Ubuntu)
Upgrade: h2
Connection: Upgrade
Last-Modified: Mon, 07 Oct 2013 03:09:42 GMT
ETag: "1bf8-4e81dfbbce980"
Accept-Ranges: bytes
Content-Length: 7160
Vary: Accept-Encoding
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Frame-Options: sameorigin
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
Referrer-Policy: strict-origin-when-cross-origin
Content-Type: image/svg+xml
and now with -H 'Accept-Encoding: gzip,deflate' to let know Apache HTTP Server that it can serve the file using compression
% curl -I -H 'Accept-Encoding: gzip,deflate' https://clementfevrier.fr/images/CC-BY-SA_icon.svg
HTTP/1.1 200 OK
Date: Fri, 20 Oct 2017 14:25:19 GMT Server: Apache/2.4.27 (Ubuntu) Upgrade: h2 Connection: Upgrade Last-Modified: Mon, 07 Oct 2013 03:09:42 GMT
ETag: "1bf8-4e81dfbbce980-gzip"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: gzip
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Frame-Options: sameorigin
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
Referrer-Policy: strict-origin-when-cross-origin
Content-Length: 3077
Content-Type: image/svg+xml
In this case, the file is more than 2 times smaller (7160÷3077 ~ 2.326941826). Enjoy!

Mastodon Follow me Mastodon Share
Comments
There is no comment yet.
Dr Clément Février

I am Dr Clément Février, French, living in Grenoble. I defended my PhD on July 4th, 2016. After my defense I run as deputy deputy (not a typo) for the national parliamentary elections in the 1st circonscription of Isère for the political movement La France Insoumise.