Caddy configuration does not meet expectations

1. The problem I’m having:

I have some API servers behind a entry server.
eg: api1:8080, api2:8080, api3:8080

There is a entry API server: api.demo.com

I want to foward request to different server by sub dir, eg:

https://api.demo.com/api1/api/v1/foo => api1:8080/api/v1/foo

https://api.demo.com/api2/api/v1/foo => api2:8080/api/v1/foo

https://api.demo.com/api3/api/v1/foo => api3:8080/api/v1/foo

But the following configuration doesn’t work

2. Error messages and/or full log output:

PASTE OVER THIS, BETWEEN THE ``` LINES.
Please use the preview pane to ensure it looks nice.

3. Caddy version:

2.6

4. How I installed and ran Caddy:

a. System environment:

b. Command:

PASTE OVER THIS, BETWEEN THE ``` LINES.
Please use the preview pane to ensure it looks nice.

c. Service/unit/compose file:

PASTE OVER THIS, BETWEEN THE ``` LINES.
Please use the preview pane to ensure it looks nice.

d. My complete Caddy config:

api.demo.com {
    log {
        output file /var/log/api.access.log
    }

    @backend {
        path_regexp backend ^/([^/]+)/(.+)
    }

    handle_path /{re.backend.1}/* {
        reverse_proxy {re.backend.1}:8080
    }
}
PASTE OVER THIS, BETWEEN THE ``` LINES.
Please use the preview pane to ensure it looks nice.

5. Links to relevant resources:

Try this: handle_path @backend {

{"level":"info","ts":1688098398.0698562,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'handle_path': /etc/caddy/Caddyfile:29 - Error during parsing: path matcher must begin with '/', got @backend

I changed it to handle_path /@backend {, still not work

That’s not what I typed :frowning: Double check that :wink:

You can’t use handle_path in that case, it only supports simple path matchers. It’s a shortcut for handle + uri strip_prefix so it doesn’t support all matchers.

You can use the long form instead like this:

@backend path_regexp backend ^/([^/]+)/(.+)
handle @backend {
	uri strip_prefix /{re.backend.1}
	reverse_proxy {re.backend.1}:8080
}
1 Like

Thank you a lot !!! :laughing:
This problem has bothered me for many days

2 Likes

Oh, oops. I totally forgot about that. :sweat_smile: Thanks Francis

1 Like

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