I want to block some spammy bots and some browsers with ad blocking based on User-agent

1. Caddy version (caddy version):

v2.5.1 h1:bAWwslD1jNeCzDa+jDCNwb8M3UJ2tPa8UZFFzPVmGKs=

2. How I run Caddy:

a. System environment:

Debian 11

b. Command:

/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile

c. Service/unit/compose file:

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

[Service]
Type=notify
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:

emby.blueseacdn.com {
    reverse_proxy localhost:8096
    @mygeofilter {
        maxmind_geolocation {
        db_path "/usr/share/GeoIP/GeoLite2-City.mmdb"
        allow_countries CN TW JP KR SG IN RU NZ MY
        allow_subdivisions UA-43
        }
    }
}

3. The problem I’m having:

I want to block some spammy bots and some browsers with ad blocking based on User-agent.

when i use:

redir {
  if_op or
  if {>User-agent} has 360
  if {>User-agent} has MicroMessenger
  if {>User-agent} has QQBrowser
  if {>User-agent} has BIDUBrowser
  if {>User-agent} has Maxthon
  if {>User-agent} has MetaSr
  if {>User-agent} has LBBROWSER
  if {>User-agent} has UBrowser
  / https://www.baidu.com/search/error.html
}

These don’t seem to work in V2, this is the only similar tutorial I can find in the Chinese internet, how should it be configured in V2? The example will redirect to the page of baidu.com, I think it may be better if the 444 code can be returned directly.

4. Error messages and/or full log output:

5. What I already tried:

redir {
  if_op or
  if {>User-agent} has 360
  if {>User-agent} has MicroMessenger
  if {>User-agent} has QQBrowser
  if {>User-agent} has BIDUBrowser
  if {>User-agent} has Maxthon
  if {>User-agent} has MetaSr
  if {>User-agent} has LBBROWSER
  if {>User-agent} has UBrowser
  / https://www.baidu.com/search/error.html
}

6. Links to relevant resources:

You defined a matcher here, but you’re not actually using it anywhere, so it’s not doing anything at all.

If you want to use this to block these clients, you’ll need to attach it to a handler directive to do something with those requests. Maybe something like this:

emby.blueseacdn.com {
	@mygeofilter {
		maxmind_geolocation {
			db_path "/usr/share/GeoIP/GeoLite2-City.mmdb"
			allow_countries CN TW JP KR SG IN RU NZ MY
			allow_subdivisions UA-43
		}
	}

	handle @mygeofilter {
		reverse_proxy localhost:8096
	}

	# Fallback for otherwise not matched requests
	handle {
		abort
	}
}

Yeah, that’s Caddy v1 syntax. In Caddy v2, you define a request matcher, then attach it to a handler. You can use the header_regexp matcher to write a rule to match values of the User-Agent header.

1 Like

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