Skip to content

Question: how to get a custom banned page for crowdsec #87

@huyz

Description

@huyz

(This question would fit better under Discussions but there is no Discussions section enabled on this project.)

I'm trying to get a custom banned web page to be returned in case of CrowdSec bouncing, but I'm confused by the format of Caddyfile and can't get the right ordering and nesting to work. Or maybe it's not possible because of the way this crowdsec module works. Anyone know if it's even possible or what I'm doing wrong?

{
	# This is for crowdsec snippet because handle_errors doesn't
	# have a default order (and it can't be placed inside of `route`)
	# I tried `first`, `before route`, and `after route`.
	order handle_errors first

	# Per https://github.com/hslatman/caddy-crowdsec-bouncer/#usage
	crowdsec {
		api_url http://crowdsec:8080
		api_key {{ CROWDSEC_API_KEY }}
		ticker_interval 15s
		#appsec_url http://crowdsec:7422
		#disable_streaming
		#enable_hard_fails
	}
}

http://*.mydomain.com {
	@myhost host myhost.mydomain.com

    handle @myhost {
		# This catches CrowdSec 403s (middleware level)
		# Note that certain directives, for example reverse_proxy which may write a response with an
		# HTTP status which is classified as an error, will not trigger the error routes.
		handle_errors {
			@crowdsec_blocked expression {err.status_code} == 403
			handle @crowdsec_blocked {
				root /srv
				rewrite * /banned.html
				file_server
			}
	
			# Handle other actual errors (5xx, etc.)
			respond "Error {err.status_code}: {err.status_text}" {err.status_code}
		}
	
		route {
			crowdsec
			#appsec

			reverse_proxy myhost:8080 {
				header_up X-Real-IP {http.request.header.X-Forwarded-For}
				header_up X-Forwarded-For "{http.request.header.X-Forwarded-For}, {remote_host}"
				header_up X-Forwarded-Proto https
	
				header_down Referrer-Policy "strict-origin-when-cross-origin"
			}
		}
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions