Setting header on a requested file during or after a rewrite

1. Output of caddy version:

v2.6.2

2. How I run Caddy:

a. System environment:

b. Command:

caddy run

d. My complete Caddy config:

{
debug
}

(hashcache) {
        map {path} {uncached} {uncachedbool} {
                ~(?i)(/static|/themes)(/.*)\.([a-z0-9]{8,40})\.([a-zA-Z0-9]+)$ "${1}${2}.${4}" "yes"
                default "no"
        }
        @uncached_map expression `{uncachedbool} == "yes"`
        rewrite @uncached_map {uncached}
}

(expiresTest) {
        @matchExpiresTenDays {
                # use file directive to not set header on non existing file.
                file
                path *.png
        }
        header @matchExpiresTenDays Cache-Control max-age=864000
}

:8000 {
      root * .
      import hashcache
      import expiresTest
      file_server
}

3. The problem I’m having:

I want to set a header on a static resource that gets rewritten.

4. Error messages and/or full log output:

2022/12/14 14:40:36.971	DEBUG	http.handlers.rewrite	rewrote request	{"request": {"remote_ip": "127.0.0.1", "remote_port": "65362", "proto": "HTTP/1.1", "method": "HEAD", "host": "localhost:8000", "uri": "/static/test.32523452352345.png", "headers": {"User-Agent": ["curl/7.84.0"], "Accept": ["*/*"]}}, "method": "HEAD", "uri": "/static/test.png"}
2022/12/14 14:40:36.971	DEBUG	http.handlers.file_server	sanitized path join	{"site_root": ".", "request_path": "/static/test.png", "result": "static/test.png"}
2022/12/14 14:40:36.971	DEBUG	http.handlers.file_server	opening file	{"filename": "static/test.png"}

5. What I already tried:

Request the existing file as is - the Cache-Control with intended max-age is set:

# curl -I http://localhost:8000/static/test.png
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: max-age=864000
Content-Length: 22417
Content-Type: image/png
Etag: "rmvx8phap"
Last-Modified: Wed, 14 Dec 2022 14:22:01 GMT
Server: Caddy
Date: Wed, 14 Dec 2022 14:42:10 GMT

Request the existing file in a way it is rewritten - Cache-Control is missing:

# curl -I http://localhost:8000/static/test.32523452352345.png
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 22417
Content-Type: image/png
Etag: "rmvx8phap"
Last-Modified: Wed, 14 Dec 2022 14:22:01 GMT
Server: Caddy
Date: Wed, 14 Dec 2022 14:42:19 GMT

That’s the correct behavior; you’ll want to apply the header before rewriting, if your intention is to apply the header to the request as it was before it was rewritten. That’s why rewriting is useful: you can apply certain routes by synthetic changes to the requests that otherwise wouldn’t qualify.

Try doing this:

route {
    import expiresTest
    import hashcache
}

Thanks for the explanation!

It’s sometimes hard for me to understand how exactly the internal flow is, because on the surface everything seems linear. Sure, a rewrite has to lead to a change in the second run, that’s just how things have to work, it’s similar with other webservers too. It’s still a bit of a search for clues now and then.

An “Explanation tab” like at “regex101.com” would be sensational :laughing: You enter a request and uncle Caddy explains what happens :upside_down_face:

Yeah, that would be cool. For now, there’s

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.