Need help on path_regexp config

1. Caddy version (caddy version): 2.4.6

2. How I run Caddy:

# desired Caddy version
ARG VERSION=2.4.6

###########
# builder #
FROM caddy:${VERSION}-builder-alpine AS builder
ARG VERSION
RUN xcaddy build v${VERSION} \
    --with github.com/caddy-dns/cloudflare

#########
# image #
FROM caddy:${VERSION}-alpine
COPY --from=builder /usr/bin/caddy /usr/bin/caddy

a. System environment:

OS Information 	linux x86_64 Slackware 14.2 x86_64 (post 14.2 -current)
Kernel Version 	5.10.28-Unraid
Total CPU 	16
Total memory 	16.8 GB
Version 	20.10.5 (API: 1.41)
Root directory 	/var/lib/docker
Storage Driver 	btrfs
Logging Driver 	json-file
Volume Plugins 	local
Network Plugins 	bridge, host, ipvlan, macvlan, null, overlay

b. Command:

Paste command here.

c. Service/unit/compose file:

version: '3.9'

services:
  caddy:
    image: erfianugrah/caddy-cfdns:v1.2-2.4.6
    hostname: caddy
    container_name: caddy
    restart: unless-stopped
    network_mode: host
    privileged: true

    volumes:
      - /mnt/user/data/caddy/Caddyfile:/etc/caddy/Caddyfile
      - /mnt/user/data/caddy/site:/srv
      - /mnt/user/data/caddy/data:/data
      - /mnt/user/data/caddy/config:/config
    environment:
      - TZ=Asia/Singapore
      - CF_API_TOKEN=${CF_API_TOKEN}
      - EMAIL=${EMAIL}

d. My complete Caddyfile or JSON config:

{
	email {env.EMAIL}
	acme_dns cloudflare {env.CF_API_TOKEN}
	cert_issuer acme
	http_port 80
	https_port 443
	admin localhost:2019
	debug
	grace_period 5s
	log {
		level debug
		output file /var/log/access.log {
			roll_size 1gb
			roll_keep 5
			roll_keep_for 720h
		}
	}
	servers {
		protocol {
			strict_sni_host
		}
	}
}

erfianugrah.com {
	reverse_proxy 172.18.0.2:2368
	header cache-control public, max-age=86400, stale-while-revalidate=86400 {
		path_regexp ^.*\.(jpe?g|png|gif|web)
	}
}

port.erfianugrah.com {
	reverse_proxy 172.17.0.3:9000
}

servarr.erfianugrah.com {
	reverse_proxy localhost:90
}

plex.erfianugrah.com {
	reverse_proxy 172.19.0.8:32400
}

hydra.erfianugrah.com {
	reverse_proxy 172.19.0.7:5076
}

nzb.erfianugrah.com {
	reverse_proxy 172.19.0.6:7000
}

sonarr.erfianugrah.com {
	reverse_proxy 172.19.0.3:8989
}

radarr.erfianugrah.com {
	reverse_proxy 172.19.0.2:7878
}

bazarr.erfianugrah.com {
	reverse_proxy 172.19.0.4:6767
}

lidarr.erfianugrah.com {
	reverse_proxy 172.19.0.5:8686
}

nextcloud.erfianugrah.com {
	reverse_proxy 172.21.0.2:80
}

grafana-unraid.erfianugrah.com {
	reverse_proxy 172.17.0.2:3000
}

prom-unraid.erfianugrah.com {
	reverse_proxy 172.17.0.4:9090
}

3. The problem I’m having:

Setting it up this way is probably wrong, so what I wanna do is to only set headers that match this path that’s matched on this regex:

erfianugrah.com {
	reverse_proxy 172.18.0.2:2368
	header cache-control public, max-age=86400, stale-while-revalidate=86400 {
		path_regexp ^.*\.(jpe?g|png|gif|web)
	}
}

4. Error messages and/or full log output:

5. What I already tried:

erfianugrah.com {
	@image {
		path_regexp ^.*\.(jpe?g|png|gif|web)
	}
	header @image Cache-Control max-age=86400
	reverse_proxy 172.18.0.2:2368
}

6. Links to relevant resources:

erfianugrah.com {
	@image path_regexp image ^.*\.(jpe?g|png|gif|web)$
	header @image {
		Cache-Control "public, max-age=31536000, stale-while-revalidate=31536000"
		Access-Control-Allow-Origin "*"
		Access-Control-Max-Age "86400"
	}
	reverse_proxy 172.18.0.2:2368
}

This works, but it just adds another Cache-control header, need to use the replace function I guess?

According to: header (Caddyfile directive) — Caddy Documentation, what I did should work.

**<field>** is the name of the header field. By default, will overwrite any existing field of the same name. Prefix with `+` to add the field instead of replace, or prefix with `-` to remove the field.

But I get this:
image

1 Like

Yeah if you want to change the value, I think you can do this:

Cache-Control .* "new value"

But also I think you’ll have to turn on the defer option to make sure it runs after proxying (otherwise it will replace “nothing” since there’s nothing in the response before the proxy runs)

1 Like

Defer solved it, thanks!

2 Likes