Hello! I’m trying to use haproxy to cache an API response that includes a last-modified header.
I must be missing something in my configuration, because It appears that haproxy doesn’t query the backend to validate the cached response. Instead, it responds with the cached response until max-age expires.
Here are the bits from my haproxy config:
cache api-cache
total-max-size 200
max-object-size 2000000
max-age 1200
process-vary on
frontend www-https
acl cacheable_api path -m beg /api/type-info
http-request cache-use api-cache if cacheable_api
http-response cache-store api-cache
Here is a request that shows the haproxy response:
✸ http --print=Hh --verify no https://my-server/api/type-info
GET /api/type-info HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, zstd
Connection: keep-alive
Host: my-server
User-Agent: HTTPie/3.2.4
HTTP/1.1 200 OK
age: 450
content-language: en-US
content-type: application/json;charset=UTF-8
date: Thu, 12 Jun 2025 11:18:20 GMT
last-modified: Thu, 12 Jun 2025 11:18:20 GMT
transfer-encoding: chunked
x-cache: Hit from haproxy
In the response above, haproxy is responding with data that should be invalidated. On the backend server, the last-modified time has changed by two seconds. I can see this if I set a bogus Authorization header to avoid the haproxy cache:
✸ http --print=Hh --verify no https://my-server/api/type-info Authorization:foo
GET /api/type-info HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, zstd
Authorization: foo
Connection: keep-alive
Host: my-server
User-Agent: HTTPie/3.2.4
HTTP/1.1 200 OK
content-language: en-US
content-type: application/json;charset=UTF-8
date: Thu, 12 Jun 2025 11:21:12 GMT
last-modified: Thu, 12 Jun 2025 11:18:22 GMT
transfer-encoding: chunked
x-cache: Miss from haproxy
Am I missing a piece of configuration that would tell haproxy to call the backend with if-modified-since every time, to ensure the cache is valid?
1 post - 1 participant