Movatterモバイル変換


[0]ホーム

URL:


Modules |Directives |FAQ |Glossary |Sitemap

Apache HTTP Server Version 2.4

<-
Apache >HTTP Server >Documentation >Version 2.4 >Modules

Apache Module mod_brotli

Available Languages: en  | fr 

Description:Compress content via Brotli before it is delivered to theclient
Status:Extension
Module Identifier:brotli_module
Source File:mod_brotli.c
Compatibility:Available in version 2.4.26 and later.

Summary

Themod_brotli module provides theBROTLI_COMPRESS output filter that allows output from your server to be compressed using the brotli compression format before being sent to the client over the network. This module uses the Brotli library found athttps://github.com/google/brotli.

Support Apache!

Topics

Directives

Bugfix checklist

See also

top

Sample Configurations

Compression and TLS

Some web applications are vulnerable to an information disclosure attack when a TLS connection carries compressed data. For more information, review the details of the "BREACH" family of attacks.

This is a simple configuration that compresses common text-based content types.

Compress only a few types

AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript
top

Enabling Compression

Compression and TLS

Some web applications are vulnerable to an information disclosure attack when a TLS connection carries compressed data. For more information, review the details of the "BREACH" family of attacks.

Output Compression

Compression is implemented by theBROTLI_COMPRESSfilter. The following directive will enable compression for documents in the container where it is placed:

SetOutputFilter BROTLI_COMPRESSSetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-brotli

If you want to restrict the compression to particular MIME types in general, you may use theAddOutputFilterByType directive. Here is an example of enabling compression only for the html files of the Apache documentation:

<Directory "/your-server-root/manual">    AddOutputFilterByType BROTLI_COMPRESS text/html</Directory>

Note

TheBROTLI_COMPRESS filter is always inserted after RESOURCE filters like PHP or SSI. It never touches internal subrequests.

Note

There is an environment variableno-brotli, set viaSetEnv, which will disable brotli compression for a particular request, even if it is supported by the client.
top

Dealing with proxy servers

Themod_brotli module sends aVary: Accept-Encoding HTTP response header to alert proxies that a cached response should be sent only to clients that send the appropriateAccept-Encoding request header. This prevents compressed content from being sent to a client that will not understand it.

If you use some special exclusions dependent on, for example, theUser-Agent header, you must manually configure an addition to theVary header to alert proxies of the additional restrictions. For example, in a typical configuration where the addition of theBROTLI_COMPRESS filter depends on theUser-Agent, you should add:

Header append Vary User-Agent

If your decision about compression depends on other information than request headers (e.g. HTTP version), you have to set theVary header to the value*. This prevents compliant proxies from caching entirely.

Example

Header set Vary *
top

Serving pre-compressedcontent

Sincemod_brotli re-compresses content each time a request is made, some performance benefit can be derived by pre-compressing the content and telling mod_brotli to serve them without re-compressing them. This may be accomplished using a configuration like the following:

<IfModule mod_headers.c>    # Serve brotli compressed CSS files if they exist    # and the client accepts brotli.    RewriteCond "%{HTTP:Accept-encoding}" "br"    RewriteCond "%{REQUEST_FILENAME}\.br" "-s"    RewriteRule "^(.*)\.css"              "$1\.css\.br" [QSA]    # Serve brotli compressed JS files if they exist    # and the client accepts brotli.    RewriteCond "%{HTTP:Accept-encoding}" "br"    RewriteCond "%{REQUEST_FILENAME}\.br" "-s"    RewriteRule "^(.*)\.js"               "$1\.js\.br" [QSA]    # Serve correct content types, and prevent double compression.    RewriteRule "\.css\.br$" "-" [T=text/css,E=no-brotli:1]    RewriteRule "\.js\.br$"  "-" [T=text/javascript,E=no-brotli:1]    <FilesMatch "(\.js\.br|\.css\.br)$">      # Serve correct encoding type.      Header append Content-Encoding br      # Force proxies to cache brotli &      # non-brotli css/js files separately.      Header append Vary Accept-Encoding    </FilesMatch></IfModule>
top

BrotliAlterETagDirective

Description:How the outgoing ETag header should be modified during compression
Syntax:BrotliAlterETag AddSuffix|NoChange|Remove
Default:BrotliAlterETag AddSuffix
Context:server config, virtual host
Status:Extension
Module:mod_brotli

TheBrotliAlterETag directive specifies how the ETag hader should be altered when a response is compressed.

AddSuffix

Append the compression method onto the end of the ETag, causing compressed and uncompressed representations to have unique ETags. In another dynamic compression module, mod_deflate, this has been the default since 2.4.0. This setting prevents serving "HTTP Not Modified" (304) responses to conditional requests for compressed content.

NoChange

Don't change the ETag on a compressed response. In another dynamic compression module, mod_deflate, this has been the default prior to 2.4.0. This setting does not satisfy the HTTP/1.1 property that all representations of the same resource have unique ETags.

Remove

Remove the ETag header from compressed responses. This prevents some conditional requests from being possible, but avoids the shortcomings of the preceding options.

top

BrotliCompressionMaxInputBlockDirective

Description:Maximum input block size
Syntax:BrotliCompressionMaxInputBlockvalue
Default:(automatic)
Context:server config, virtual host
Status:Extension
Module:mod_brotli

TheBrotliCompressionMaxInputBlock directive specifies the maximum input block size between 16 and 24, with the caveat that larger block sizes require more memory.

top

BrotliCompressionQualityDirective

Description:Compression quality
Syntax:BrotliCompressionQualityvalue
Default:BrotliCompressionQuality 5
Context:server config, virtual host
Status:Extension
Module:mod_brotli

TheBrotliCompressionQuality directive specifies the compression quality (a value between 0 and 11). Higher quality values result in better, but also slower compression.

top

BrotliCompressionWindowDirective

Description:Brotli sliding compression window size
Syntax:BrotliCompressionWindowvalue
Default:BrotliCompressionWindow 18
Context:server config, virtual host
Status:Extension
Module:mod_brotli

TheBrotliCompressionWindow directive specifies the brotli sliding compression window size (a value between 10 and 24). Larger window sizes can improve compression quality, but require more memory.

top

BrotliFilterNoteDirective

Description:Places the compression ratio in a note for logging
Syntax:BrotliFilterNote [type]notename
Context:server config, virtual host
Status:Extension
Module:mod_brotli

TheBrotliFilterNote directive specifies that a note about compression ratios should be attached to the request. The name of the note is the value specified for the directive. You can use that note for statistical purposes by adding the value to youraccess log.

Example

BrotliFilterNote ratioLogFormat '"%r" %b (%{ratio}n) "%{User-agent}i"' brotliCustomLog "logs/brotli_log" brotli

If you want to extract more accurate values from your logs, you can use thetype argument to specify the type of data left as a note for logging.type can be one of:

Input
Store the byte count of the filter's input stream in the note.
Output
Store the byte count of the filter's output stream in the note.
Ratio
Store the compression ratio (output/input * 100) in the note. This is the default, if thetype argument is omitted.

Thus you may log it this way:

Accurate Logging

BrotliFilterNote Input instreamBrotliFilterNote Output outstreamBrotliFilterNote Ratio ratioLogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' brotliCustomLog "logs/brotli_log" brotli

See also

Available Languages: en  | fr 

top

Comments

Notice:
This is not a Q&A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Libera.chat, or sent to ourmailing lists.

Copyright 2025 The Apache Software Foundation.
Licensed under theApache License, Version 2.0.

Modules |Directives |FAQ |Glossary |Sitemap


[8]ページ先頭

©2009-2025 Movatter.jp