Create a configuration (Caddyfile) block template

Hello,

I have three domains on my caddy server and I would like to re-use the same configuration for all of them. How can I do that in Caddyfile so I don’t have duplicate configuration allover the place?

As you can see in in example everything except root/proxy is duplicated which sucks. Is there any way to avoid this?

Example (Caddyfile):

domain1.com {
    root /var/www/domain1.com

    expires {
        match .js$ 1y
        match .css$ 1y
        match .html 5i
    }
    header / {
        Strict-Transport-Security "max-age=31536000;"
        X-XSS-Protection "1; mode=block"
        X-Content-Type-Options "nosniff"
        X-Frame-Options "DENY"
        -Server
    }
    log /var/log/caddy/access.log
    error /var/log/caddy/error.log
    gzip
}

domain2.com {
    proxy / localhost:8081

    expires {
        match .js$ 1y
        match .css$ 1y
        match .html 5i
    }
    header / {
        Strict-Transport-Security "max-age=31536000;"
        X-XSS-Protection "1; mode=block"
        X-Content-Type-Options "nosniff"
        X-Frame-Options "DENY"
        -Server
    }
    log /var/log/caddy/access.log
    error /var/log/caddy/error.log
    gzip
}

You can’t. See Using headers and others globally

Off-topic, I would recommend to separate log and error log from each domain.

Okey, I will use https://caddyserver.com/docs/import then.

What does the -Server header do?

It removes the Server header. :wink:

1 Like

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