Skip to content

Instantly share code, notes, and snippets.

@marcofbb
Created April 3, 2020 04:57
Show Gist options
  • Save marcofbb/610300615b419d066efd7589cf0649a8 to your computer and use it in GitHub Desktop.
Save marcofbb/610300615b419d066efd7589cf0649a8 to your computer and use it in GitHub Desktop.
varnish limit rate request for seconds with cloudflare
## ## Not complete default.vcl code
# Install https://github.com/varnish/varnish-modules
import vsthrottle;
# If I want to implement limitation to any request (do not declare req.http.X-Actual-IP again in other subsequent subroutines)
sub vcl_recv {
# GET REAL IP USER from proxy CLOUDFLARE
set req.http.X-Actual-IP = regsub(req.http.X-Forwarded-For, "[, ].*$", "");
if(vsthrottle.is_denied(req.http.X-Actual-IP, 50, 5s, 60s)) {
# Client has exceeded 50 reqs per 5s.
# When this happens, block altogether for the next 60s.
return (synth(429, "Too Many Requests"));
}
}
# If I want to implement limitation to requests that should be cached, but not found
sub vcl_miss {
# GET REAL IP USER from proxy CLOUDFLARE
set req.http.X-Actual-IP = regsub(req.http.X-Forwarded-For, "[, ].*$", "");
if(vsthrottle.is_denied(req.http.X-Actual-IP, 50, 5s, 60s)) {
# Client has exceeded 50 reqs per 5s.
# When this happens, block altogether for the next 60s.
return (synth(429, "Too Many Requests"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment