10.2. Setup

  1. To setup a cache, you must define a cache section and use it in a proxy with
  2. the corresponding http-request and response actions.

10.2.1. Cache section

cache

  1. Declare a cache section, allocate a shared cache memory named <name>, the
  2. size of cache is mandatory.

total-max-size

  1. Define the size in RAM of the cache in megabytes. This size is split in
  2. blocks of 1kB which are used by the cache entries. Its maximum value is 4095.

max-object-size

  1. Define the maximum size of the objects to be cached. Must not be greater than
  2. an half of "total-max-size". If not set, it equals to a 256th of the cache size.
  3. All objects with sizes larger than "max-object-size" will not be cached.

max-age

  1. Define the maximum expiration duration. The expiration is set has the lowest
  2. value between the s-maxage or max-age (in this order) directive in the
  3. Cache-Control response header and this value. The default value is 60
  4. seconds, which means that you can't cache an object more than 60 seconds by
  5. default.

10.2.2. Proxy section

http-request cache-use [ { if | unless } ]

  1. Try to deliver a cached object from the cache <name>. This directive is also
  2. mandatory to store the cache as it calculates the cache hash. If you want to
  3. use a condition for both storage and delivering that's a good idea to put it
  4. after this one.

http-response cache-store [ { if | unless } ]

  1. Store an http-response within the cache. The storage of the response headers
  2. is done at this step, which means you can use others http-response actions
  3. to modify headers before or after the storage of the response. This action
  4. is responsible for the setup of the cache storage filter.

Example:

  1. backend bck1
  2. mode http
  3. http-request cache-use foobar
  4. http-response cache-store foobar
  5. server srv1 127.0.0.1:80
  6. cache foobar
  7. total-max-size 4
  8. max-age 240

HAProxy 2.0.14 – Configuration Manual
2020/04/02, willy tarreau