If Else Handler Matching

1. Caddy version (caddy version):

v2.1.1

2. How I run Caddy:

Directly on debian buster amd64

a. System environment:

Debian Buster amd64

b. Command:

caddy run

d. My complete Caddyfile or JSON config:

*.website.org {
	tls {
		dns cloudflare {$CLOUDFLARE_API_KEY}
	}

	@sub1 {
host sub1.website.org 
remote_ip 192.168.1.3
}

	handle @sub1 {
		reverse_proxy 127.0.0.1:9000
	}


	@sub2 host sub2.website.org
	handle @sub2 {
		reverse_proxy 127.0.0.1:8000
	}
}

3. The problem I’m having:

I want to create an else statement for requests that dont match the sub1 matcher, this should respond with a 403 error. I am not sure the handler syntax I need for this.

5. What I already tried:

I have tried experimenting with the ‘not’ keyword but I want to know if there is a more concise way to go about it.

Could you be more specific about what you want to happen? Are you trying to make anything that doesn’t match your sub1 matcher get proxied to port 8000, i.e. what you have for sub2 currently? Or are you looking to add a third handler to do something else?

The not matcher is likely what you want, but I don’t have a good picture of what exactly you want so I can’t give a precise answer.

Hi! Yes, I am trying to have one IP (192.168.1.3) be able to access sub1.website.org and have access to the service running on port 9000. However I want other users (anyone not 192.168.1.3) to receive a 403 Error. I have experimented with the respond 403 directive but I am not sure if I need to create 2 matchers to handle this.

You could just do this:

@blocked {
	host sub1.website.org
	not remote_ip 192.168.1.3
}
respond @blocked "Access Denied" 403
2 Likes

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