Need help migrating to Caddyfile v2 from Caddyfile v1

Hi I’ve been using Caddy v1 since its inception. I’d like to start using Caddy v2 and I’m getting stuck with fixing my Caddyfile.

  1. What is the equivalent of the index?

I’d like to convert this v1 Caddyfile to v2:

server.com {
   root /data
   index default.txt
}

I tried

server.com {
   root * /data
   file_server * {
      index default.txt
    }
}

but it complains.

  1. What is the equivalent of except?

I’d like to convert this v1 Caddyfile to v2:

server.com {
        root /www/hostyoself
        proxy / 127.0.0.1:8010 {
                except /static
        }
}

to v2. I can’t seem to find any info about exceptions. Also, would I use the file_server to do this instead of just using root?

  1. Is there an equivalent of timeouts? I.e. I’d like to turn them off for some sites.

  2. What is the equivalent of basicauth?

I’d like to convert this v1 Caddyfile to v2:

server.com {
   proxy / 127.0.0.1:8002
   basicauth /secure user plainpassword
}
  1. What is the equivalent of errors?

I’d like to convert this v1 Caddyfile to v2:

server.com {
        root /www/data
        errors {
                404 404.html
        }
}
  1. What is the equivalent of redir?

I’d like to convert this v1 Caddyfile to v2:

server.com {
      root /www/data
      redir {
                /2015/04/18 /software/ 302
                /movies/ https://othersite 302
      }
}

You’re right, that looks correct. @matt, possible bug on index

I’m seeing this error:

adapt: parsing caddyfile tokens for 'file_server': Caddyfile:4 - Error during parsing: Wrong argument count or unexpected line ending after 'default.txt'

I think the v2 approach for this would be:

try_files {path} default.txt

This should serve a file at the requested path if it exists, otherwise it defaults to default.txt, if I’m understanding what you’re trying to achieve.

You would use a matcher to exclude the the path. In v2, matchers are the primary way to implement conditional behaviour.

@notStatic {
    not {
        path /static/*
    }
}
reverse_proxy @notStatic 127.0.0.1:8010

The reverse_proxy has the subdirective transport which contains some timeout options. See reverse_proxy (Caddyfile directive) — Caddy Documentation

@notStatic {
    not {
        path /static/*
    }
}
reverse_proxy @notStatic 127.0.0.1:8010 {
    transport http {
        dial_timeout 30s
        tls_timeout 10s
    }
}

The direct equivalents to timeouts from Caddy v1 isn’t yet available in the Caddyfile, but it should be very soon (next few days or weeks). For now, they’re configurable via JSON. See JSON Config Structure - Caddy Documentation

			"read_timeout": 0,
			"read_header_timeout": 0,
			"write_timeout": 0,
			"idle_timeout": 0,

For now, they’re defaults are no timeout.

The basicauth directive! :stuck_out_tongue: https://caddyserver.com/docs/caddyfile/directives/basicauth

It doesn’t support plaintext passwords anymore, you can use the caddy hash-password command to hash the password for you.

basicauth /secure/* {
	username JDJhJDEwJEVCNmdaNEg2Ti5iejRMYkF3MFZhZ3VtV3E1SzBWZEZ5Q3VWc0tzOEJwZE9TaFlZdEVkZDhX
}

Unfortunately, that’s still up in the air, hasn’t been totally determined how it’ll look in the Caddyfile. Should be figured out very soon though. The discussion so far seems to point to it looking something like:

handle_errors {
    @404 {
        # some matcher on the response code, TBD
    }
    rewrite @404 404.html
}

Doing this is possible in JSON currently using the "errors" key in a server block: JSON Config Structure - Caddy Documentation

The redir directive! :stuck_out_tongue: redir (Caddyfile directive) — Caddy Documentation

redir /movies/* https://othersite.com 302
redir /2015/04/18/* /software/ 302
3 Likes

The Caddyfile parsing bug was actually an error checking bug, and has now been fixed at file_server: Fix dumb error check I must have written at 1am · caddyserver/caddy@673d3d0 · GitHub. Build artifacts will be available shortly that you can try.

2 Likes

Build artifacts for the latest commit are here: file_server: Fix dumb error check I must have written at 1am · caddyserver/caddy@673d3d0 · GitHub :slightly_smiling_face:

@francislavoie Thanks, what a stunning answer. I’m going to give it another try :slight_smile: Your answer really illuminated the power behind v2, I think I am understanding it now.

@matt Thank you for that quick fix!!

Y’all are amazing.

2 Likes

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