Does it matter if the log directive is first or last?

1. The problem I’m having:

Will there be a difference in logging if the log directive comes first or last in a Caddyfile? I changed the order in a config for one of our servers but can’t really tell if there is a difference.

I’ve always used Caddy with the impression it doesn’t matter, but a few investigations made be doubt my knowledge…

2. Error messages and/or full log output:

Not relevant

3. Caddy version:

2.7.6

4. How I installed and ran Caddy:

Not relevant, this is a general config question

Caddy config:

Let’s say I have a Caddyfile with two handle_path directives. Will there be a difference with these two configs?

Example 1:

my.lovely.domain.com {
    log
    tls hello@lovely.domain.com
    encode zstd, gzip
    handle_path /cdn/* {
        root * /var/caddy/cdn
        file_server
    }
    handle_path /frontend/* {
        root * /var/caddy/frontend
        file_server
    }
    respond "nothing here for you"
}

Example 2:

my.lovely.domain.com {
    tls hello@lovely.domain.com
    encode zstd, gzip
    handle_path /cdn/* {
        root * /var/caddy/cdn
        file_server
    }
    handle_path /frontend/* {
        root * /var/caddy/frontend
        file_server
    }
    respond "nothing here for you"
    log
}

Is this a case of “Order doesn’t matter”?
Or am I fooled and missed a lot of successful requests?

It doesn’t matter, no.

Caddy sorts directives according to the directive order, but, log (and tls, bind, handle_errors, request_body) is not a handler directive, it’s site-scoped configuration directive, so it doesn’t need to have an order.

If you adapt your config to JSON with caddy adapt -p you’ll see how your config differs (or doesn’t differ) based on how you lay it out in your Caddyfile.

2 Likes

Thanks Francis, then I still remembered it correctly.

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