mod_compress: Lighttpd Gzip Compression To Improve Download and Browsing Speed

Gzip is the most popular and effective compression method. Most modern web browser supports and accepts compressed data transfer. By gziping response time can reduced by 60-70% as compare to normal web page. The end result is faster web site experience for both dial up (they’re not dead yet – I’ve dial up account for backup purpose) and broadband user. I’ve already written about speeding up Apache 2.x web access or downloads with mod_deflate.

mod_compress for Lighttpd 1.4.xx

Lighttpd 1.4.xx supports gzip compression using mod_compress. This module can reduces the network load and can improve the overall throughput of the webserver. All major http-clients support compression by announcing it in the Accept-Encoding header as follows:

Accept-Encoding: gzip, deflate

If lighttpd sees this header in the request, it can compress the response using one of the methods listed by the client. The web server notifies the web client of this via the Content-Encoding header in the response:

Content-Encoding: gzip

This is used to negotiate the most suitable compression method. Lighttpd support deflate, gzip and bzip2.

Configure mod_compress

Open your lighttpd.conf file:
# vi /etc/lighttpd/lighttpd.conf
Append mod_compress to server.modules directive:
server.modules += ( "mod_compress" )
Setup compress.cache-dir to stored all cached file:
compress.cache-dir = "/tmp/lighttpdcompress/"
Finally, define mimetypes to get compressed. Following will allow to compress javascript, plain text files, css file,xml file etc:

compress.filetype           = ("text/plain","text/css", "text/xml", "text/javascript" )

Save and close the file. Create /tmp/lighttpdcompress/ file:
# mkdir -p /tmp/lighttpdcompress/
# chown lighttpd:lighttpd /tmp/lighttpdcompress/

Restart lighttpd:
# /etc/init.d/lighttpd restart

How do I enable mod_compress per virtual host?

Use conditional $HTTP host directive, for example turn on compression for

$HTTP["host"] =~ "theos\.in" {
  compress.cache-dir = "/var/www/cache/"

PHP dynamic compression

Open file:
# vi /etc/php.ini
To compress dynamic content with PHP please enable following two directives:
zlib.output_compression = On
zlib.output_handler = On

Save and close the file. Restart lighttpd:
# service lighttpd restart

Cleaning cache directory

You need to run a shell script for cleaning out cache directory.

See also:

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
13 comments… add one
  • Mr Person Dec 6, 2012 @ 15:21

    # /etc/init.d/lighttpd restart
    is a typo

    I think you meant
    # /etc/rc.d/lighttpd restart

    or more portable:
    service lighttpd restart

    • Mr Person Dec 6, 2012 @ 15:21

      sorry /usr/local/etc/rc.d/lighttpd restart

  • JarekMk Mar 21, 2011 @ 11:00

    @nerd – use www-data – lighty is working on this user.

  • nerd Feb 2, 2011 @ 8:28

    when i do chown lighttpd:lighttpd /tmp/lighttpdcompress/
    i get chown: lighttpd: Invalid argument

  • Nico Jul 7, 2010 @ 19:48

    Thank you ! Perfect for me !

  • Craig M. Rosenblum Mar 26, 2009 @ 10:24

    How do i get this to work under windows? I keep getting errors in the folder specification…Any suggestions?

  • Ulrich Jan 10, 2009 @ 23:07

    You should use “application/x-javascript”. “text/javascript” doesn’t work for me.

  • mumuri Dec 13, 2008 @ 23:15

    There is little spelling mistake “mod_comress “

  • Ryan Dec 2, 2008 @ 7:03

    THanks for this 🙂

    Worked perfectly for my server

  • wYke Nov 29, 2008 @ 9:23

    hi, I have turned on the zlib compression and handler but it seems the apache load average shoots up to more than 200. how is it supposed to help?

  • TheCaptain Nov 21, 2008 @ 18:53

    Yes it doesn’t work for me too..
    I’m running CentOS, and have set everything as your instruction but when I check using Online GZIP Tester ( it showed that it still uncompressed. Or maybe this tool only works for apache ?

  • China pictures Oct 17, 2008 @ 3:25

    What is the diference with the deflate mod ?

  • 高达00 Oct 6, 2008 @ 12:41

    I use exactly the settings like that, but the mod_compress seems not working with my Ubuntu box, which runs lighttpd 1.4.18 version. Anybody could tell me why?

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.