Directing bots to a reverse proxy

The issue is due to a misconception about the relationship between matchers and directives. Directives that support matchers take matchers, but not the other way around. I recommend you go over the Caddyfile Concepts — Caddy Documentation page. You will need to use named matchers.

Secondly, the header matcher is case sensitive. To go around this aspect of the header matcher, you need to use the alternative header_regexp matcher. The regular expression engine is Go’s regexp engine whose syntax is detailed here: syntax package - regexp/syntax - pkg.go.dev. I believe this is the matcher you need:

@bot {
    header_regexp User-Agent (?i)(bot|python)
}

Since you want either one of the routing decisions to take place, then you need to wrap them in handle directive. Thus your final Caddyfile is:

preview.minty.art {
	@bot header_regexp User-Agent (?i)(bot|python)

	handle @bot {
		reverse_proxy http://192.168.1.1
	}

	handle {
		root * /Users/mohammed/projects
		file_server browse
	}
	log {
		output file /var/log/preview.log
	}
}
5 Likes