Location of rate_limit directive

I’m using GitHub - mholt/caddy-ratelimit: HTTP rate limiting module for Caddy 2 and it works well.

The only minor drawback is you can’t define matchers via the Caddyfile. As a workaround, I’ve considered a pattern like:

handle @critical {
	rate_limit {
		zone critical {
			...
		}
	}
}
handle {
	rate_limit {
		zone all {
			...
		}
	}
}

This seems to work perfectly. Are there any pitfalls - performance or otherwise - involved with using the rate_limit directive within handle blocks?

Thanks guys.

No pitfalls. Nanoseconds at worst.

But that might just be an implementation oversight, I don’t see a reason it shouldn’t support inline matchers. I suggest you open an issue on the repo to ask for it to be fixed.

2 Likes

That’s odd. It calls RegisterHandlerDirective() and so it should automatically support a matcher.

No wait, I definitely just tried it with a matcher and it works fine. Something else must be wrong. Please fill out the Help template completely, I don’t think it’s a bug.

1 Like

Are you sure the matcher is applied? The readme states that this isn’t implemented:

PLANNED:

  • Ability to define matchers in zones with Caddyfile

(The Caddyfile does not yet support defining matchers for RL zones, so that has been omitted from this example.)

For specific zones, but the rate limit handler should still support matchers…

Ah, I get you. Confirmed that it works. Just a more succinct way of doing it I guess.

Because it didn’t feature in the docs, I didn’t realise that rate_limit also accepted an inline matcher. Not sufficiently Caddy-native yet!

Just to clarify for anyone else interested, you can do:

rate_limit /api/* {
	zone api {
		key static
		events 25
		window 1s
	}
}
2 Likes

Guess I just didn’t think about it, so I didn’t put it in the readme. But yeah it should work for every handler module since Caddy does the matching, not the modules. Thanks for following up!

1 Like

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