compress.txt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. ==================
  2. Output Compression
  3. ==================
  4. --------------------
  5. Module: mod_compress
  6. --------------------
  7. .. meta::
  8. :keywords: lighttpd, compress
  9. .. contents:: Table of Contents
  10. WARNING
  11. =======
  12. mod_compress has been subsumed by mod_deflate.
  13. Description
  14. ===========
  15. Output compression reduces the network load and can improve the overall
  16. throughput of the webserver. All major http-clients support compression by
  17. announcing it in the Accept-Encoding header. This is used to negotiate the
  18. most suitable compression method. We support deflate, gzip and bzip2.
  19. deflate (RFC1950, RFC1951) and gzip (RFC1952) depend on zlib while bzip2
  20. depends on libbzip2. bzip2 is only supported by lynx and some other console
  21. text-browsers.
  22. We currently limit to compression support to static files.
  23. Caching
  24. -------
  25. mod_compress can store compressed files on disk to optimize the compression
  26. on a second request away. As soon as compress.cache-dir is set the files are
  27. compressed.
  28. (You will need to create the cache directory if it doesn't already exist. The web server will not do this for you. The directory will also need the proper ownership. For Debian/Ubuntu the user and group ids should both be www-data.)
  29. The names of the cache files are made of the filename, the compression method
  30. and the etag associated to the file.
  31. Cleaning the cache is left to the user. A cron job deleting files older than
  32. 10 days could do it: ::
  33. find /var/www/cache -type f -mtime +10 | xargs -r rm
  34. Limitations
  35. -----------
  36. The module limits the compression of files to files smaller than 128 MByte and
  37. larger than 128 Byte.
  38. The lower limit is set as small files tend to become larger by compressing due
  39. to the compression headers, the upper limit is set to work sensibly with
  40. memory and cpu-time.
  41. Directories containing a tilde ('~') are not created automatically (See ticket
  42. #113). To enable compression for user dirs you have to create the directories
  43. by hand in the cache directory.
  44. Options
  45. =======
  46. compress.allowed-encodings
  47. override default set of allowed encodings
  48. e.g.: ::
  49. compress.allowed-encodings = ("bzip2", "gzip", "deflate")
  50. compress.cache-dir
  51. name of the directory where compressed content will be cached
  52. e.g.: ::
  53. compress.cache-dir = "/var/www/cache/"
  54. # even better with virt-hosting
  55. $HTTP["host"] == "docs.example.org" {
  56. compress.cache-dir = "/var/www/cache/docs.example.org/"
  57. }
  58. Default: not set, compress the file for every request
  59. compress.filetype
  60. mimetypes which might get compressed
  61. e.g.: ::
  62. compress.filetype = ("text/plain", "text/html")
  63. Keep in mind that compressed JavaScript and CSS files are broken in some
  64. browsers. Not setting any filetypes will result in no files being compressed.
  65. NOTE: You have to specify the full mime-type! If you also define a charset, for example, you have to use "text/plain; charset=utf-8" instead of just "text/plain".
  66. Default: not set
  67. compress.max-filesize
  68. maximum size of the original file to be compressed kBytes.
  69. This is meant to protect the server against DoSing as compressing large
  70. (let's say 1Gbyte) takes a lot of time and would delay the whole operation
  71. of the server.
  72. There is a hard upper limit of 128Mbyte.
  73. Default: unlimited (== hard-limit of 128MByte)
  74. Display compressed files
  75. ========================
  76. If you enable mod_compress, and you want to force clients to uncompress and display compressed text files, please force mimetype to nothing.
  77. Example :
  78. If you want to add headers for uncompress and display diff.gz files , add this section in your conf : ::
  79. $HTTP["url"] =~ "\.diff\.gz" {
  80. setenv.add-response-header = ( "Content-Encoding" => "gzip" )
  81. mimetype.assign = ()
  82. }
  83. Compressing Dynamic Content
  84. ===========================
  85. PHP
  86. ---
  87. To compress dynamic content with PHP please enable ::
  88. zlib.output_compression = 1
  89. zlib.output_handler = On
  90. in the php.ini as PHP provides compression support by itself.
  91. mod_compress of lighttpd 1.5 r1992 may not set correct Content-Encoding with php-fcgi. A solution to that problem would be:
  92. 1.disable mod_compress when request a php file::
  93. $HTTP["url"] !~ "\.php$" {
  94. compress.filetype = ("text/plain", "text/html", "text/javascript", "text/css", "text/xml")
  95. }
  96. 2.enable mod_setenv of your lighttpd::
  97. server.modules += ( "mod_setenv" )
  98. 3.manually set Content-Encoding::
  99. $HTTP["url"] =~ "\.php$" {
  100. setenv.add-response-header = ( "Content-Encoding" => "gzip")
  101. }
  102. TurboGears
  103. ----------
  104. To compress dynamic content with TurboGears please enable ::
  105. [/]
  106. gzip_filter.on = True
  107. gzip_filter.mime_types = ["application/x-javascript", "text/javascript", "text/html", "text/css", "text/plain"]
  108. in the config/app.cfg file in your TurboGears application. The above lines should already be in the file. You just need to remove the comment symbol in front of the lines to make them active.
  109. Django
  110. ------
  111. To compress dynamic content with Django please enable the GZipMiddleware ::
  112. MIDDLEWARE_CLASSES = (
  113. 'django.middleware.gzip.GZipMiddleware',
  114. ...
  115. )
  116. in the settings.py file in your Django project.
  117. Catalyst
  118. --------
  119. To compress dynamic content with Perl/Catalyst, simply use the Catalyst::Plugin::Compress::Gzip module available on CPAN ::
  120. use Catalyst qw(
  121. Compress::Gzip
  122. ...
  123. );
  124. in your main package (MyApp.pm). Further configuration is not required.
  125. }}}