lucaslorentz / caddy-docker-proxy

Caddy as a reverse proxy for Docker
MIT License
3.06k stars 174 forks source link

How to map multiple handlers? #598

Closed wsw70 closed 8 months ago

wsw70 commented 8 months ago

I would like to convert to labels the followig Caddyfile

https://sharry.swtk.eu {
    # https://eikek.github.io/sharry/doc/rest#rest-api
    route {
        handle / {
            import authenticate-with-authelia
            reverse_proxy sharry-restserver-1:9090
        }

                handle /app/login {
            import authenticate-with-authelia
            reverse_proxy sharry-restserver-1:9090
        }

                handle /app/home {
            import authenticate-with-authelia
            reverse_proxy sharry-restserver-1:9090
        }

        # fallback handling of everything else
        handle {
            reverse_proxy sharry-restserver-1:9090
        }
    }
}

I tried to find a way that follows the documentation and ended up with

      caddy: "sharry.swtk.eu"
      caddy.handle_path_0: /
      caddy.handle_path_0.0_import: "authenticate-with-authelia"
      caddy.handle_path_0.1_reverse_proxy: "sharry:9090"
      caddy.handle_path_1: /app/login
      caddy.handle_path_1.0_import: "authenticate-with-authelia"
      caddy.handle_path_1.1_reverse_proxy: "sharry:9090"
      caddy.handle_path_2: /app/home
      caddy.handle_path_2.0_import: "authenticate-with-authelia"
      caddy.handle_path_2.1_reverse_proxy: "sharry:9090"
      caddy.handle_path_3.0_reverse_proxy: "sharry:9090"

My idea is that the first underscored digit is the index of the entry in route, and for a given one, the underscored digit would be the order of statements. is this the correct approach?

I tried to make sense of the compiled configuration via the :2019/config/ JSON but it is a bit too complex for me.

wsw70 commented 8 months ago

I had it all wrong on so many levels. The correct configuration is

      caddy: "sharry.swtk.eu"
      caddy.route.0_handle: /
      caddy.route.0_handle.0_import: "authenticate-with-authelia"
      caddy.route.0_handle.1_reverse_proxy: "sharry:9090"
      caddy.route.1_handle: /app/login
      caddy.route.1_handle.0_import: "authenticate-with-authelia"
      caddy.route.1_handle.1_reverse_proxy: "sharry:9090"
      caddy.route.2_handle: /app/home
      caddy.route.2_handle.0_import: "authenticate-with-authelia"
      caddy.route.2_handle.1_reverse_proxy: "sharry:9090"
      caddy.route.3_handle.0_reverse_proxy: "sharry:9090"
francislavoie commented 8 months ago

You can simplify it by using a named matcher:

@authenticated path / /app/login /app/home
handle @authenticated {
    import authenticate-with-authelia
    reverse_proxy sharry:9090
}

handle {
    reverse_proxy sharry:9090
}

Shouldn't you use {{upstreams 9090}} instead? That way it'll auto-populate the upstreams if you have multiple replicas running etc.

And you don't need the wrapping route around the whole thing, it doesn't do anything for you.