Fall through matcher question

1. Caddy version (caddy version):

2.2.1

2. How I run Caddy:

systemd

a. System environment:

debian 10 systemd

b. Command:

paste command here

c. Service/unit/compose file:

# caddy.service
#
# For using Caddy with a config file.
#
# Make sure the ExecStart and ExecReload commands are correct
# for your installation.
#
# See https://caddyserver.com/docs/install for instructions.
#
# WARNING: This service does not use the --resume flag, so if you
# use the API to make changes, they will be overwritten by the
# Caddyfile next time the service is restarted. If you intend to
# use Caddy's API to configure it, add the --resume flag to the
# `caddy run` command or use the caddy-api.service file instead.

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

d. My complete Caddyfile or JSON config:

{
  email "wschenk@gmail.com"
}

faas.willschenk.com {
   @proxy {
          path /ui/*
          path /system/*
          path /function/*
   }

   reverse_proxy @proxy http://localhost:8080

   rewrite not { @proxy } /function/web/{uri}
}

3. The problem I’m having:

I want to have three paths reversed proxied to http://localhost:8080 and then everything else rewritten to /function/web/{uri}, so that it will get reversed proxied.

I attempted to have a not matcher to be the inverse of the @proxy named matcher, but I don’t think that works.

If I put rewrite / /function/web it will not see the reverse proxy statement and then rewrite forever. What I expect is to have the rewrite rule rewrite, and then the first rule (higher up in the file) to then proxy it.

I find the documentation for Caddy 2 to be super confusing. What is a matcher, where can I use it? What is the json file? How is that different from the Caddyfile?

4. Error messages and/or full log output:

5. What I already tried:

6. Links to relevant resources:

To implement fallthrough, I typically recommend using handle blocks for this. The handle directive allows you to write mutually exclusive handlers.

@proxy path /ui/* /system/* /function/*
handle @proxy {
	reverse_proxy localhost:8080
}

handle {
	# Fallback for anything not otherwise handled
}

But that said, what you’re actually trying to do might be best handled by inverting your matchers here so that your rewrite is done if it’s not one of those paths already, because you’re wanting to proxy either way.

@should_rewrite not path /ui/* /system/* /function/*
rewrite @should_rewrite /function/web/{path}

reverse_proxy localhost:8080

This following page in the docs covers that question:

In Caddy, the actual underlying config language is actually JSON. The Caddyfile is an “adapter”, i.e. a layer that transforms your config into a JSON config that Caddy can actually use to run. You can see the underlying JSON config for your Caddyfile by running caddy adapt.

JSON config is best to use if you need fine-grained control for configuring Caddy, but the Caddyfile is enough for 90%+ of users whose configs don’t land in the edge cases that the Caddyfile adapter doesn’t cover.

The Caddyfile is essentially a UX layer to make Caddy nicer to use :slight_smile:

2 Likes

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