-
Notifications
You must be signed in to change notification settings - Fork 14
Improve varnish config, add support for URIBAN by regex #495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ead552d
11cbd62
b8ef26d
9e62b7f
6859a11
a3e28a4
2294948
cba1254
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -183,7 +183,7 @@ data: | |||||
| set beresp.http.x-ttl = "10m"; | ||||||
| } | ||||||
|
|
||||||
| if (bereq.url ~ "^[^?]*\.(jpg|jpeg|gif|png|svg|ico|webp|css|js|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|flv|swf|html|htm|otf)(\?.*)?$") { | ||||||
| if (bereq.url ~ "^[^?]*\.(?:jpg|jpeg|gif|png|svg|ico|webp|css|js|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|flv|swf|html|htm|otf)(?:\?.*)?$") { | ||||||
| # Strip any cookies before static files are inserted into cache. | ||||||
| unset beresp.http.set-cookie; | ||||||
| if(beresp.status == 200){ | ||||||
|
|
@@ -199,7 +199,7 @@ data: | |||||
| # Large static files are delivered directly to the end-user without | ||||||
| # waiting for Varnish to fully read the file first. | ||||||
| # Varnish 4 fully supports Streaming, so use streaming here to avoid locking. | ||||||
| if (bereq.url ~ "^[^?]*\.(mp[34]|rar|tar|tgz|gz|pdf|wav|zip|bz2|xz|7z|avi|mov|ogm|mpe?g|mk[av]|webm)(\?.*)?$") { | ||||||
| if (bereq.url ~ "^[^?]*\.(?:mp[34]|rar|tar|tgz|gz|pdf|wav|zip|bz2|xz|7z|avi|mov|ogm|mpe?g|mk[av]|webm)(?:\?.*)?$") { | ||||||
| unset beresp.http.set-cookie; | ||||||
| set beresp.do_stream = true; # Check memory usage it'll grow in fetch_chunksize blocks (128k by default) if the backend doesn't send a Content-Length header, so only enable it for big objects | ||||||
| set beresp.do_gzip = false; # Don't try to compress it for storage | ||||||
|
|
@@ -304,8 +304,8 @@ data: | |||||
| return(pipe); | ||||||
| } | ||||||
|
|
||||||
| # Only allow BAN requests from IP addresses in the 'purge' ACL. | ||||||
| if (req.method == "BAN" || req.method == "URIBAN") { | ||||||
| # Only allow BAN requests from IP addressees in the 'internal' ACL. | ||||||
| if (req.method == "BAN") { | ||||||
| # Admin port is only exposed to internal network | ||||||
| if (!client.ip ~ purge) { | ||||||
| return (synth(403, "Not allowed.")); | ||||||
|
|
@@ -319,11 +319,30 @@ data: | |||||
| elseif (req.http.Cache-Tags) { | ||||||
| ban("obj.http.Cache-Tags ~ " + req.http.Cache-Tags); | ||||||
| } | ||||||
| elseif (req.method == "URIBAN") { | ||||||
| ban("req.http.host == " + req.http.host + " && req.url == " + req.url); | ||||||
| else { | ||||||
| # If there are no cache tags headers in a BAN request, | ||||||
| # it is a bad request, so indicate that to the client. | ||||||
| return (synth(400, "Cache tags headers not present.")); | ||||||
| } | ||||||
| # Throw a synthetic page so the request won't go to the backend. | ||||||
| return (synth(200, "Ban added.")); | ||||||
| } | ||||||
|
|
||||||
| # Only allow URIBAN requests from IP addressees in the 'internal' ACL. | ||||||
| if (req.method == "URIBAN") { | ||||||
| # Admin port is only exposed to internal network | ||||||
| if (!client.ip ~ purge) { | ||||||
| return (synth(403, "Not allowed.")); | ||||||
| } | ||||||
|
|
||||||
| # If x-url-invalidate-pattern header is present, | ||||||
| # use it to match URLs in stored objects. (ban by regex pattern) | ||||||
| if (req.http.x-url-invalidate-pattern) { | ||||||
| ban("obj.http.x-url ~ " + req.http.x-url-invalidate-pattern); | ||||||
|
||||||
| } | ||||||
| # Without pattern, ban by matching host and URL exactly. | ||||||
| else { | ||||||
| return (synth(403, "Cache tags headers not present.")); | ||||||
| ban("obj.http.host == " + req.http.host + " && obj.http.x-url == " + req.url); | ||||||
|
MarttiR marked this conversation as resolved.
|
||||||
| } | ||||||
| # Throw a synthetic page so the request won't go to the backend. | ||||||
| return (synth(200, "Ban added.")); | ||||||
|
|
@@ -406,7 +425,7 @@ data: | |||||
|
|
||||||
| {{- if .Values.mailpit.enabled }} | ||||||
| // No varnish for mailpit | ||||||
| if (req.url ~ "^/mailpit(/|$)") { | ||||||
| if (req.url ~ "^/mailpit(?:/|$)") { | ||||||
| return (pass); | ||||||
| } | ||||||
| {{- end }} | ||||||
|
|
@@ -420,13 +439,28 @@ data: | |||||
| return (pass); | ||||||
| } | ||||||
|
|
||||||
| if (req.url ~ "\.(png|gif|jpg|tif|tiff|ico|webp|swf|css|js|pdf|doc|xls|ppt|zip)(\?.*)?$") { | ||||||
| // Forcing a lookup with static file requests | ||||||
| if (req.http.Accept-Encoding) { | ||||||
| if (req.url ~ "\.(?:bz2|eot|gif|gz|ico|jpg|mp3|ogg|pdf|png|svg|swf|tbz|tgz|tif|tiff|ttf|webp|woff|zip)(\?.*)?$") { | ||||||
| # No point in compressing these | ||||||
| unset req.http.Accept-Encoding; | ||||||
| } elsif (req.http.Accept-Encoding ~ "gzip") { | ||||||
| set req.http.Accept-Encoding = "gzip"; | ||||||
| } elsif (req.http.Accept-Encoding ~ "deflate") { | ||||||
| set req.http.Accept-Encoding = "deflate"; | ||||||
| } else { | ||||||
| # unknown algorithm | ||||||
| unset req.http.Accept-Encoding; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (req.url ~ "\.(?:bmp|bz2|css|doc|eot|gif|ico|jpg|js|pdf|png|ppt|svg|swf|tif|tiff|ttf|webp|woff|xls|zip)$") { | ||||||
| # Static file request do not vary on cookies | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. I like this. |
||||||
| unset req.http.Cookie; | ||||||
| return (hash); | ||||||
| } | ||||||
|
|
||||||
| # Do not allow public access to cron.php , update.php or install.php. | ||||||
| if (req.url ~ "^(|/core)/(cron|install|update)\.php$" && !client.ip ~ internal) { | ||||||
| if (req.url ~ "^(?:/core)?/(?:cron|install|update)\.php$" && !client.ip ~ internal) { | ||||||
| # Have Varnish throw the error directly. | ||||||
| return (synth( 404, "Page not found.")); | ||||||
| } | ||||||
|
|
@@ -445,12 +479,7 @@ data: | |||||
| } | ||||||
|
|
||||||
| if (req.http.Cookie) { | ||||||
| if (req.url ~ "\.(png|gif|jpg|svg|tif|tiff|ico|webp|swf|css|js|pdf|doc|xls|ppt|zip|woff|eot|ttf|bmp|bz2)$") { | ||||||
| # Static file request do not vary on cookies | ||||||
| unset req.http.Cookie; | ||||||
| return (hash); | ||||||
| } | ||||||
| elseif (req.http.Cookie ~ "(SESS[a-z0-9]+|SSESS[a-z0-9]+)") { | ||||||
| if (req.http.Cookie ~ "S?SESS[a-z0-9]+=") { | ||||||
|
||||||
| if (req.http.Cookie ~ "S?SESS[a-z0-9]+=") { | |
| if (req.http.Cookie ~ "(S?SESS[a-z0-9]+=)") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.

Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider "IP not allowed".
Reasoning: suppose someone gets the error, and has no clue of this code here, it could be a guesswork why the error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK, the message set here is only logged in varnishlog, not transmitted via HTTP.