Could not migrate v1 Caddyfile to v2

1. Caddy version (caddy version):

2.0.0

2. How I run Caddy:

caddy run

a. System environment:

$ uname -a
Darwin C02ZL0RJLVDN 18.7.0 Darwin Kernel Version 18.7.0: Mon Feb 10 21:08:45 PST 2020; root:xnu-4903.278.28~1/RELEASE_X86_64 x86_64

b. Command:

caddy run

c. Service/unit/compose file:

None

d. My complete Caddyfile or JSON config:

v1 Caddyfile I used and it works well.

:80 {
  gzip
  log ./access.log

  proxy /api/* 127.0.0.1:9005

  root ./public
  rewrite / {
    if {path} not_match ^/api
    to {path} /
  }
}

v2 Caddyfile I wrote.

:80 {
  encode gzip
  log {
    output file ./access.log
    level DEBUG
  }

  reverse_proxy /api/* 127.0.0.1:9005

  @noneapi {
    not path /api/*
  }
  root * public
  file_server
  rewrite @noneapi /
}

3. The problem I’m having:

After I run caddy run, I can access the /api/* correctly, but the other path is not working, the response content is correct, but the content types are wrong.

So any know how can I create a equivalent Caddyfile in v2?

4. Error messages and/or full log output:

caddy run
2020/05/13 07:27:02.999	INFO	using adjacent Caddyfile
2020/05/13 07:27:03.002	INFO	admin	admin endpoint started	{"address": "tcp/localhost:2019", "enforce_origin": false, "origins": ["localhost:2019", "[::1]:2019", "127.0.0.1:2019"]}
2020/05/13 07:27:03.003	INFO	http	server is listening only on the HTTP port, so no automatic HTTPS will be applied to this server	{"server_name": "srv0", "http_port": 80}
2020/05/13 15:27:03 [INFO][cache:0xc0008d64b0] Started certificate maintenance routine
2020/05/13 07:27:03.003	INFO	tls	cleaned up storage units
2020/05/13 07:27:03.003	INFO	autosaved config	{"file": "/Users/xxxx/Library/Application Support/Caddy/autosave.json"}
2020/05/13 07:27:03.003	INFO	serving initial configuration

5. What I already tried:

None

6. Links to relevant resources:

What this means is “if the path doesn’t start with /api, then try to see if {path} exists on disk, otherwise rewrite to /.”

This is not exactly equivalent. This is saying “if the path doesn’t start with /api/, then rewrite to /.”

You’re missing the part that tries to serve {path} if it exists on disk, so ALL non-API requests are rewritten to the root, instead of just the ones that are unknown.

Instead, I’d write it like this:

:80 {
  root ./public

  encode gzip
  log {
    output file ./access.log
    level DEBUG
  }

  @unknown {
    not path /api/*
    not file
  }
  rewrite @unknown /

  reverse_proxy /api/* 127.0.0.1:9005

  file_server
}

This will make sure the rewrite only happens if the file doesn’t exist on disk. The file matcher implicitly checks whether {path} exists.

I took the liberty of rearranging things a bit as well - I think it makes the most sense to arrange the Caddyfile to roughly match the directive order in which the Caddyfile is sorted.

2 Likes

Thank you @francislavoie this is a great explanation, and the problem solved after I updated the Caddyfile per your suggestion.

1 Like

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