You could probably accomplish that in one line, but it is clearer like this:
frontend myfrontend
bind 0.0.0.0:80
default_backend default
acl cache_me path_dir /js
acl cache_me path_dir /images
use_backend cache if cache_me
backend default
server server1 1.2.3.4:80
backend cache
http-request set-header cache-control max-age="2592000"
server server1 1.2.3.4:80
Explanation:
the acl
keyword tells haproxy that it should add the request to a specific acl if the condition hits.
path_dir
matches a subdirectory, whereas path
would match the entire path. Maybe path_sub
is better here, it looks for a substring in the path.
use_backend
directs requests to a specific backend if the request is in the ACL. Everything else goes to the default backend.
This way, you can easily add more paths later, or even point those requests to different servers later if you wanted.
Additionally, filtering by domain as well:
frontend myfrontend
bind 0.0.0.0:80
default_backend default
acl cache_me path_dir /js
acl cache_me path_dir /images
acl domain1 hdr(host) -m sub example.com
use_backend cache if cache_me and domain1
backend default
server server1 1.2.3.4:80
backend cache
http-request set-header cache-control max-age="2592000"
server server1 1.2.3.4:80