Filtering user agents with named matcher errors

1. Caddy version:

v2.6.2 h1:wKoFIxpmOJLGl3QXoo6PNbYvGW4xLEgo32GPBEjWL8o=

2. The relevant part of the caddy config {
    import "botblock/bots.list"
    rewrite @isBot /foobar


@isBot `{>User-Agent}.contains("360Spider") || {>User-Agent}.contains("404checker") || ... || false`

(bot UAs taken from ultimate apache bot blocker, the || false only exists for lazyness reasons, because I’m not editing like 200 bots by hand)

3. The problem I’m having:

Hey there,
I’m currently trying to redirect bots to a url, but it just does not work. For some reason, caddy does not accept the {>User-Agent} placeholder which I found.
The following just errors out:

Syntax error: mismatched input '}' expecting ':'
Syntax error: extraneous input '>' expecting [...]

This repeats for a few tries before it gives up.

Why does this not work? How should it be done?

That’s a Caddy v1 header placeholder.

In v2, you should use {header.User-Agent} instead.

Thank you!

Why is this not mentioned anywhere in the caddy documentation? I searched for user agent several times and nothing showed up.

Because User-Agent is just one particular HTTP header field out of an infinite amount of possible header fields. There’s no reason for Caddy to document that particular header, because it’s not relevant to the workings of the server.

Your question should be “how do I get the value for a header field”, not “how do I get the value for User-Agent”.

I have to admit this is all quite new territory to me. Caddy itself at least makes it much much easier to set up a webserver with absolutely no experience than nginx does.

I only know what the documentation sais. If the documentation does not mention a particular header, I’m confused about what to do. I hope you understand.

HTTP headers are arbitrary key-value pairs. The “key” (header field) can be anything. It can be Foo, it can be Something-Completely-Arbitrary, it can be User-Agent, it can be Authorization, etc. Infinite possibilities.

For that reason, it’s mathematically impossible to document “every header”. That’s not Caddy’s job. But MDN HTTP headers - HTTP | MDN does a pretty good job of documenting most of the well-known HTTP headers and what they’re meant for.

This isn’t a problem with Caddy’s documentation, it’s just that you haven’t learned those aspects of how HTTP works.

In the docs, we have to make reasonable assumptions about the reader’s understanding of the fundamentals. We have to assume that you know how HTTP works. Otherwise we’d be wasting a lot of time in the docs re-iterating things that most people probably already know. As writers of the docs, we have to spend our time wisely, and only document things that actually matter. And that’s specifics about Caddy.

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