Use snippets to make Caddyfile better - need assistance pls 🙏

1. Caddy version (caddy version):

v2.4.3

2. How I run Caddy:

caddy runs as a service (autostart-ed)

a. System environment:

No Docker… DietPi v7.3.2

b. Command:

service caddy status
service caddy reload

c. Service/unit/compose file:

d. My complete Caddyfile or JSON config:

# The Caddyfile - to configure your Caddy web server.
#
http://emupay.me:9992 {
  redir https://emupay.me:8443{uri}
}
#
https://emupay.me:8443 {
  tls /etc/caddy/emufullchain1.pem /etc/caddy/emuprivkey1.pem
  encode gzip
  header {
          Etag
          cache-control: public, max-age=3600
         } 
  root * /var/www/html/ira
  file_server
}
#
http://emupay.me:9993 {
  redir https://emupay.me:8444{uri}
}
#
https://emupay.me:8444 {
  tls /etc/caddy/emufullchain1.pem /etc/caddy/emuprivkey1.pem
  encode gzip 
  header {
          Etag
          cache-control: public, max-age=3600
         } 
  root * /var/www/html/mj
  file_server
}
#

3. The problem I’m having:

All is working (for my test sites). But as I will add many more sites I want to make it efficient to write, rather that repetitive of same information…

4. Error messages and/or full log output:

No errors.

5. What I already tried:

I cannot understand how to use snippets for this, but it seems from the doc that the solution lies there.

6. Links to relevant resources:

Hi all Caddyfile experts :pray: I think I should be using Snippets or Global settings… rather than repeating same lines many times. My Caddyfile is working for my test static sites - I have about 10 more actual static sites to add. Perhaps somebody could please advise how to rewrite my Caddyfile so it uses snippets - or how ever else that would be “best case”, f that s not snippets . Thank in advance :blush:

Here’s one possible approach…

(init) {
  tls /etc/caddy/emufullchain1.pem /etc/caddy/emuprivkey1.pem
  encode gzip
  header {
    Etag
    cache-control: public, max-age=3600
  } 
}
# The Caddyfile - to configure your Caddy web server.
#
http://emupay.me:9992 {
  redir https://emupay.me:8443{uri}
}
#
https://emupay.me:8443 {
  import init
  root * /var/www/html/ira
  file_server
}
#
http://emupay.me:9993 {
  redir https://emupay.me:8444{uri}
}
#
https://emupay.me:8444 {
  import init
  root * /var/www/html/mj
  file_server
}
#
4 Likes

thank you so much !

1 Like

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