How to configure a specific route

1. The problem I’m having:

I have following Caddy config:

(blitzNoQueryString) {
      @blitzCache {
                method GET
                not expression {query} != ''
        }
        route @blitzCache {
                try_files /cache/blitz/{host}{uri}/index.html {path} {path}/index.php?{query}
        }
}

So if the url doesnt contains any req. params, the blitzCache route will be used. What I actually want is that the blitzCache route should be used regardless of any req. params but those following two:

.) x-craft-live-preview
.) x-craft-preview

May somboady has a hint how to achieve this?

Option 1: Both x-craft-live-preview AND x-craft-preview are not present at the same time:

 @blitzCache {
    method GET
    not query x-craft-live-preview=* x-craft-preview=*
}

For example,

  • ?x-craft-live-preview will match
  • ?x-craft-preview will match
  • ?x-craft-live-preview&x-craft-preview won’t match
  • ?anything-else will match

Option 2: Neither x-craft-live-preview NOR x-craft-preview is present:

 @blitzCache {
    method GET
    not query x-craft-live-preview=*
    not query x-craft-preview=*
}

For example,

  • ?x-craft-live-preview won’t match
  • ?x-craft-preview won’t match
  • ?x-craft-live-preview&x-craft-preview won’t match
  • ?anything-else will match

Hi, I tried option 2, but still caddy skips the blitzCache route if any req. params is present. Any ideas?

Hi, I tried option 2 but still caddy skips the blitzCache route if any req. param is present. Any ideas?

I can’t seem to reproduce your issue, sorry.

Here’s my quick-n-dirty Caddyfile:

{
	http_port 8080
}

:8080 {
	@blitzCache {
		method GET
		not query x-craft-live-preview=*
		not query x-craft-preview=*
	}
	route @blitzCache {
		respond "Route 'blitzCache': yes"
	}
	respond "Route 'blitzCache': no"
}
$ caddy run --config Caddyfile

And here are my curl tests:

$ curl http://localhost:8080
Route 'blitzCache': yes

$ curl http://localhost:8080/?foo=bar
Route 'blitzCache': yes

$ curl http://localhost:8080/?x-craft-live-preview
Route 'blitzCache': no

$ curl 'http://localhost:8080/?x-craft-live-preview&foo=bar'
Route 'blitzCache': no

$ curl http://localhost:8080/?x-craft-preview
Route 'blitzCache': no

$ curl 'http://localhost:8080/?x-craft-preview&foo=bar'
Route 'blitzCache': no

$ curl 'http://localhost:8080/?x-craft-live-preview&x-craft-preview'
Route 'blitzCache': no

$ curl 'http://localhost:8080/?x-craft-live-preview&x-craft-preview&foo=bar'
Route 'blitzCache': no

As long as the request doesn’t include x-craft-live-preview or x-craft-preview, I get:

Route 'blitzCache': yes

Otherwise, I get:

Route 'blitzCache': no

So, the routing logic based on the presence (or absence) of x-craft-live-preview or x-craft-preview seems to be working fine on my end.

Hi, I know now why it doesnt work at my end, we have a reverse proxy setup so our query params are stored in http.request.header.x-orig-query

But caddy throws an error for following config block.

@blitzCache {
method GET
not {http.request.header.x-orig-query} x-craft-live-preview=*
not {http.request.header.x-orig-query} x-craft-preview=*
}

{“level”:“info”,“ts”:1740404912.2124543,“msg”:“using config from file”,“file”:“/data/etc/Caddyfile”}
Error: adapting config using caddyfile: getting matcher module ‘{http.request.header.x-orig-query}’: module not registered: http.matchers.{http.request.header.x-orig-query}, at /xxx/Caddyfile:41 import chain [‘/xxx/Caddyfile:79 (import blitzNoQueryString)’]

May somebody knows how to solve this?

1 Like

This is invalid. What is the not supposed to act on? The not matcher takes another matcher to negate. What matcher is it taking?

You need to check either header or var

2 Likes

What @Mohammed90 said here - use either header or var.

Caddyfile

{
	http_port 8080
}

:8080 {
	@blitzCache {
		method GET
		not header x-orig-query *x-craft-live-preview*
		not header x-orig-query *x-craft-preview*
	}
	route @blitzCache {
		respond "Route 'blitzCache': yes"
	}
	respond "Route 'blitzCache': no"
}
$ caddy run --config Caddyfile

Test:

$ curl http://localhost:8080
Route 'blitzCache': yes

$ curl http://localhost:8080 -H 'x-orig-query: foo=bar'
Route 'blitzCache': yes

$ curl http://localhost:8080 -H 'x-orig-query: x-craft-live-preview'
Route 'blitzCache': no

$ curl http://localhost:8080 -H 'x-orig-query: x-craft-live-preview&foo=bar'
Route 'blitzCache': no

$ curl http://localhost:8080 -H 'x-orig-query: x-craft-live-preview&x-craft-preview'
Route 'blitzCache': no

$ curl http://localhost:8080 -H 'x-orig-query: x-craft-live-preview&x-craft-preview&foo=bar'
Route 'blitzCache': no