How do I block a user agent globally

1. The problem I’m having:

I want to use header_regexp to match user agents globally to block them

{
        @go_http_client header User-Agent Go-http-client/1.1
        handle @go_http_client {
                abort
        }
        log {
                output file /var/log/caddy/caddy.log
                format json
                exclude admin
        }
}

(Yes, I know this is wrong. Just an example)

2. Error messages and/or full log output:

unrecognized global option: @go_http_client

3. Caddy version:

v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk=

a. System environment:

5.15.0-113-generic #123-Ubuntu SMP x86_64 GNU/Linux

5. Links to relevant resources:

The global options section isn’t for applying handlers globally. It’s for specific configuration that are global to the process/server. See this page:

You can use snippets to define the recurring handling pattern, and then import it within the sites blocks.

1 Like

Feels slightly redundant to copy/paste when there are a lot of sites but I suppose it might be time for me to just generate my Caddyfile from code. Thanks

I mean, snippets are scripted copy-paste for you. Just one import line in each site.

1 Like

Yeah using that now. Still tedious when you have 100+ sites