3

1. Caddy version (caddy version): 2.0.0

2. How I run Caddy:

I use Docker (docker-compose) to run Caddyserver. Here is the command of my caddy service: command: ["caddy", "run", "--config", "/etc/caddy/Caddyfile"]

a. System environment:

Docker version 19.03.1 docker-compose version 1.22.0

b. Command:
docker-compose -f docker-compose.caddy.yml up -d --build
c. Service/unit/compose file:
version: "3.4"
services:
  # other services...
  caddy:
    image: caddy:2.0.0
    restart: always
    container_name: caddy
    deploy:
      replicas: 2
      update_config:
        delay: 10s
      restart_policy:
        condition: on-failure
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - www:/var/www/html
      - caddy_data:/data
      - caddy_config:/config
    ports:
      - "80:80"
      - "443:443"
    command: ["caddy", "run", "--config", "/etc/caddy/Caddyfile"]

volumes:
  db-data:
  conf:
  www:
  cert:
  caddy_data:
  caddy_config:
d. My complete Caddyfile or JSON config:
:443 {
  root * /var/www/html/{host}
  file_server
  tls {
    on_demand
  }
  reverse_proxy /sites/action/* api:4000
}

3. The problem I'm having:

I run a platform where users can host their websites, so I have plenty of domains hosted into the server, each domain has its own directory. Whenever visitors hit into a domain I look into the parent directory by the {host} parameter and serve its files. It works totally fine until I want to redirect non-www to www or www to non-www. This is not my problem as well, the problem arises when I don't know whether the domain should redirect to www or non-www!

When the domains are created by the users some of them may choose to forward to www and some of them may choose to forward to non-www. I can create a system file depending on that choice (eg: redirect-rules.conf). I need a way to read the content of that file and execute it in Caddyfile.

Let's say a domain directory example.com was created and chosen www to be forwarded into example.com. I am thinking of a pseudo redirect-rules.conf file that contains: redir www.example.com example.com

4. Error messages and/or full log output:

I tried to read a conf file using import directive like this: import /var/www/html/{host}/redir_rules. But this configuration is wrong because I am not allowed to use variable into a file path! The error I get: Error during parsing: File to import not found: /var/www/html/{host}/redir

What would be the ideal solution for this scenario?

5. What I already tried:

import a conf file from root, seems like it import directive reads file from absolute path.

6. Links to relevant resources:

Anwar Hussain
  • 450
  • 2
  • 12

0 Answers0