Configuring HAProxy cache-store to only cache specific file types

I’m building a new load balancer and need to configure the cache to only store certain file types. At the moment, my config only wants to cache everything small enough to fit. I’ve tried setting up a variable based on the request and calling that in the cache-store statement, but based on the behavior of the service, it’s caching everything.

We configured a test box to pull from and included a delay in the response so that we’d be able to determine if the response was from the test server or the cache. We can also see the curls in the access log if it’s actually hitting the server. I’m not concerned that my test is faulty; I have clear proof when a request hits the server vs when it’s pulled from cache.

Here are the relevant portions of my config

frontend <redacted>
        bind <IP>:80
        bind <IP>:443 ssl crt <certificate.pem> ssl-min-ver TLSv1.2 alpn h2,http/1.1

        http-request set-var(req.path) hdr(Path) if { path_end .jpeg .jpg .png .gif .svg .ico .css .less .ttf .eot }

        acl cachedata path_end .jpg .jpeg .png .gif .svg .ico .css .less .ttf .eot

        http-request add-header X-Forwarded-Proto https if { ssl_fc }
        http-request cache-use lb if cachedata
        http-response cache-store lb if { var(req.path) -m end .jpg .jpeg .png .gif .svg .ico .css .less .ttf .eot }

        redirect scheme https code 301 if !{ ssl_fc }

        default_backend <redacted>

cache lb
        total-max-size 1024
        max-age 600

Go to Source
Author: Brad R