1.. Licensed to the Apache Software Foundation (ASF) under one
2   or more contributor license agreements.  See the NOTICE file
3   distributed with this work for additional information
4   regarding copyright ownership.  The ASF licenses this file
5   to you under the Apache License, Version 2.0 (the
6   "License"); you may not use this file except in compliance
7   with the License.  You may obtain a copy of the License at
8
9   http://www.apache.org/licenses/LICENSE-2.0
10
11   Unless required by applicable law or agreed to in writing,
12   software distributed under the License is distributed on an
13   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14   KIND, either express or implied.  See the License for the
15    specific language governing permissions and limitations
16   under the License.
17
18.. include:: ../../common.defs
19
20.. _admin-plugins-compress:
21
22Compress Plugin
23***************
24
25This plugin adds compression and decompression options to both origin and cache
26responses.
27
28Purpose
29=======
30
31Not all clients can handle compressed content. Not all origin servers are
32configured to respond with compressed content when a client says it can accept
33it. And it's not always necessary to make two separate requests to an origin,
34and track two separate cache objects, for the same content - once for a
35compressed version and another time for an uncompressed version.
36
37This plugin tidies up these problems by transparently compressing or deflating
38origin responses, as necessary, so that both variants of a response are stored
39as :term:`alternates <alternate>` and the appropriate version is used for client responses,
40depending on the client's indication (via an ``Accept`` request header) of what
41it can support.
42
43Additionally, this plugin adds configurability for what types of origin
44responses will receive this treatment, which will be proxied and cached with
45default behavior, and which may be explicitly disallowed to cache both
46compressed and deflated versions (because, for example, the cost of compression
47is known ahead of time to outweigh the space and bandwidth savings and you wish
48to avoid |TS| even testing for the possibility).
49
50Installation
51============
52
53This plugin is considered stable and is included with |TS| by default. There
54are no special steps necessary for its installation.
55
56Configuration
57=============
58
59This plugin can be used as either global plugin or remap plugin.
60It can be enabled globally for |TS| by adding the following to your
61:file:`plugin.config`::
62
63   compress.so
64
65With no further options, this will enable the following default behavior:
66
67*  Enable caching of both compressed and uncompressed versions of origin
68   responses as :term:`alternates <alternate>`.
69
70*  Compress objects with `text/*` content types for every origin.
71
72*  Don't hide `Accept` encoding headers from origin servers (for an offloading
73   reverse proxy).
74
75*  No URLs are disallowed from compression.
76
77*  Disable flush (flush compressed content to client).
78
79* Only objects greater than 1Kb will be compressed
80
81Alternatively, a configuration may be specified (shown here using the sample
82configuration provided with the plugin's source)::
83
84   compress.so <path-to-plugin>/sample.compress.config
85
86This can be used as remap plugin by pointing to config file in remap rule
87:file:`remap.config`::
88
89   @plugin=compress.so @pparam=sample.compress.config
90
91The following sections detail the options you may specify in the plugin's
92configuration file. Options may be used globally, or may be specified on a
93per-site basis by preceding them with a `[<site>]` line, where `<site>` is the
94client-facing domain for which the options should apply.
95
96Per site configuration for remap plugin should be ignored.
97
98cache
99-----
100
101When set to ``true``, causes |TS| to cache both the compressed and uncompressed
102versions of the content as :term:`alternates <alternate>`. When set to
103``false``, |TS| will cache only the compressed or decompressed variant returned
104by the origin. Enabled by default.
105
106range-request
107-------------
108
109When set to ``true``, causes |TS| to compress responses to Range Requests.
110Disabled by default. Setting this to true while setting cache to false leads to delivering corrupted content.
111
112compressible-content-type
113-------------------------
114
115Provides a wildcard to match against content types, determining which are to be
116considered compressible. This defaults to ``text/*``. Takes one Content-Type
117per line.
118
119compressible-status-code
120------------------------
121
122A comma separated list of response status codes for which to enable
123compression. Defaults to 200, 206, 304.
124
125minimum-content-length
126----------------------
127
128Minimum Content-Length value sent by the origin server to consider the response
129compressible. Due to the overhead and latency of compression and decompression,
130it only makes sense to compress files above a certain size threshold.
131Compressing files below 150 bytes can actually make them larger. This setting
132only applies if the response explicitly sends Content-Length. Regardless of
133this setting, responses with ``Content-Length: 0`` are considered not
134compressible.
135
136allow
137--------
138
139Provides a wildcard pattern which will be applied to request URLs. Any which
140match the pattern will be considered compressible, and only deflated versions
141of the objects will be cached and returned to clients. This may be useful for
142objects which already have their own compression built-in, to avoid the expense
143of multiple rounds of compression for trivial gains. If the regex is preceded by
144``!`` (for example ``allow !*/nothere/*``), it disables the plugin from those machine URLs.
145
146enabled
147-------
148
149When set to ``true`` (the default) permits objects to be compressed, and when ``false``
150effectively disables the plugin in the current context.
151
152flush
153-----
154
155Enables (``true``) or disables (``false``) flushing of compressed objects to
156clients. This calls the compression algorithm's mechanism (Z_SYNC_FLUSH and for gzip
157and BROTLI_OPERATION_FLUSH for brotli) to send compressed data early.
158
159remove-accept-encoding
160----------------------
161
162When set to ``true`` this option causes the plugin to strip the request's
163``Accept-Encoding`` header when contacting the origin server. Setting this option to ``false``
164will leave the header intact if the client provided it.
165
166*  To ease the load on the origins.
167
168*  For when the proxy parses responses, and the resulting compression and
169   decompression is wasteful.
170
171supported-algorithms
172----------------------
173
174Provides the compression algorithms that are supported, a comma separate list
175of values. This will allow |TS| to selectively support ``gzip``, ``deflate``,
176and brotli (``br``) compression. The default is ``gzip``. Multiple algorithms can
177be selected using ',' delimiter, for instance, ``supported-algorithms
178deflate,gzip,br``. Note that this list must **not** contain any white-spaces!
179
180Note that if :ts:cv:`proxy.config.http.normalize_ae` is ``1``, only gzip will
181be considered, and if it is ``2``, only br or gzip will be considered.
182
183Examples
184========
185
186To establish global defaults for all site requests passing through |TS|, while
187overriding just a handful for requests to content at ``www.example.com``, you
188might create a configuration with the following options::
189
190   # Set some global options first
191   cache true
192   remove-accept-encoding false
193   compressible-content-type text/*
194   compressible-content-type application/json
195   compressible-status-code 200, 206
196   minimum-content-length 860
197   flush false
198
199   # Now set a configuration for www.example.com
200   [www.example.com]
201   cache false
202   remove-accept-encoding true
203   allow !/notthis/*.js
204   allow /this/*.js
205   flush true
206
207   # Allows brotli encoded response from origin but is not capable of brotli compression
208   [brotli.allowed.com]
209   enabled true
210   compressible-content-type text/*
211   compressible-content-type application/json
212   flush true
213   supported-algorithms gzip,deflate
214
215   # Supports brotli compression
216   [brotli.compress.com]
217   enabled true
218   compressible-content-type text/*
219   compressible-content-type application/json
220   flush true
221   supported-algorithms br,gzip
222
223   # This origin does it all
224   [bar.example.com]
225   enabled false
226
227   # A reasonable list of content-types that are compressible
228   compressible-content-type text/*
229   compressible-content-type *font*
230   compressible-content-type *javascript
231   compressible-content-type *json
232   compressible-content-type *ml;*
233   compressible-content-type *mpegURL
234   compressible-content-type *mpegurl
235   compressible-content-type *otf
236   compressible-content-type *ttf
237   compressible-content-type *type
238   compressible-content-type *xml
239   compressible-content-type application/eot
240   compressible-content-type application/pkix-crl
241   compressible-content-type application/x-httpd-cgi
242   compressible-content-type application/x-perl
243   compressible-content-type image/vnd.microsoft.icon
244   compressible-content-type image/x-icon
245
246Assuming the above options are in a file at ``/etc/trafficserver/compress.config``
247the plugin would be enabled for |TS| in :file:`plugin.config` as::
248
249   compress.so /etc/trafficserver/compress.config
250
251Alternatively, the compress plugin can be used as a remap plugin: ::
252
253   map http://www.example.com http://origin.example.com \
254      @plugin=compress.so @pparam=compress.config
255
256   $ cat /etc/trafficserver/compress.config
257   enabled true
258   cache true
259   compressible-content-type *xml
260   supported-algorithms
261