Header not being overwritten

1. Caddy version (caddy version):

v2.4.4 h1:QBsN1jXEsCqRpKPBb8ebVnBNgPxwL50HINWWTuZ7evU=

2. How I run Caddy:

service caddy start

a. System environment:

ubuntu 20.04.1

b. Command:

service caddy start

d. My complete Caddyfile or JSON config:

staging01.ktbyte.com {
    file_server {
        root /home/ubuntu/earth-landing
    }
    handle_errors {
        reverse_proxy http://localhost:4567 {
            lb_try_duration 60s
            fail_duration 1s
            health_uri /health
            health_interval 1s
        }
        route {
            header /resources/* Cache-Control max-age=86400
        }
    }
}

3. The problem I’m having:

I am trying to replace my cache-control header for a specific path. But here’s what I see - two cache-control headers:

curl -sSL -D - https://staging01.ktbyte.com/resources/dist/assets/images/logos/newnewlogo.png -o /dev/null | grep -i cache-control
cache-control: max-age=86400
cache-control: private, no-cache, must-revalidate

If I skip Caddy and access my server directly you can see the second line:

curl -sSL -D - http://staging01.ktbyte.com:4567/resources/dist/assets/images/logos/newnewlogo.png -o /dev/null | grep -i cache-control
Cache-Control: private, no-cache, must-revalidate

5. What I already tried:

I tried putting the route after file_server, but that didn’t help. /resources is actually handled by the error_handler anyway.

I tried removing the header first, with header /resources/* -Cache-Control. That didn’t help either.

6. Links to relevant resources:

I read the docs. They seem to say that if I did header /resources/* +Cache-Control max-age=86400 then I’d see the behaviour I’m getting - add a header, not replace it. But I don’t have the plus.

I have a solution.

staging01.ktbyte.com {
    file_server {
        root /home/ubuntu/earth-landing
    }
    handle_errors {
        reverse_proxy http://localhost:4567 {
            lb_try_duration 60s
            fail_duration 1s
            health_uri /health
            health_interval 1s
            header_down -Cache-Control
        }
        header /resources/* Cache-Control max-age=86400
    }
}

This has the side-effect of removing Cache-Control from all non-resource paths from my reverse_proxy too, but I don’t care about that, so I’m happy with this.

1 Like

Please upgrade to v2.4.5! It’s available in the apt repo.


If I understand your config, I think you probably want to do something more like this, instead of relying on file_server emitting errors to proxy.

staging01.ktbyte.com {
	root * /home/ubuntu/earth-landing

	@fileExists file
	handle @fileExists {
		file_server
	}

	handle {
		header /resources/* Cache-Control max-age=86400
		reverse_proxy http://localhost:4567 {
			lb_try_duration 60s
			fail_duration 1s
			health_uri /health
			health_interval 1s
			header_down -Cache-Control
		}
	}
}

This uses the file matcher to check if the file exists before using file_server. This is better, because it doesn’t abuse error routes, and would let you use handle_errors for actual errors, for example like if your proxy upstream is down, you can display an error page.

Thanks for the tip. That works, but only if I access /index.html. I fixed it by adding this line: try_files {path} {path}/index.html.

Right, you can actually use the file matcher with that:

	@fileExists file {path} {path}/index.html
	handle @fileExists {
		file_server
	}

Doesn’t do a rewrite, instead just matches on those paths and lets file_server do its usual index files behaviour

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