Advertisement
Bazze

Varnish 3.x VCL

Aug 10th, 2013
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. backend apache {
  2. .host = "127.0.0.1";
  3. .port = "8008";
  4. }
  5. acl purge {
  6. "localhost";
  7. "127.0.0.1";
  8. }
  9. sub vcl_recv {
  10. // Strip cookies for static files:
  11. if (req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$") {
  12. unset req.http.Cookie;
  13. return(lookup);
  14. }
  15. // Remove has_js and Google Analytics __* cookies.
  16. set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|has_js)=[^;]*", "");
  17. // Remove a ";" prefix, if present.
  18. set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
  19. // Remove empty cookies.
  20. if (req.http.Cookie ~ "^\s*$") {
  21. unset req.http.Cookie;
  22. }
  23. if (req.request == "PURGE") {
  24. if (!client.ip ~ purge) {
  25. error 405 "Not allowed.";
  26. }
  27. ban("req.url ~ " + req.url + " && req.http.host == " + req.http.host);
  28. error 200 "Purged.";
  29. }
  30. }
  31. sub vcl_hash {
  32. if (req.http.Cookie) {
  33. hash_data(req.http.Cookie);
  34. }
  35. }
  36. sub vcl_fetch {
  37. // Strip cookies for static files:
  38. if (req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$") {
  39. unset beresp.http.set-cookie;
  40. }
  41. // Varnish determined the object was not cacheable
  42. if (!(beresp.ttl > 0s)) {
  43. set beresp.http.X-Cacheable = "NO:Not Cacheable";
  44. } elsif(req.http.Cookie ~"(UserID|_session)") {
  45. // You don't wish to cache content for logged in users
  46. set beresp.http.X-Cacheable = "NO:Got Session";
  47. return(hit_for_pass);
  48. } elsif ( beresp.http.Cache-Control ~ "private") {
  49. // You are respecting the Cache-Control=private header from the backend
  50. set beresp.http.X-Cacheable = "NO:Cache-Control=private";
  51. return(hit_for_pass);
  52. } elsif ( beresp.ttl < 1s ) {
  53. // You are extending the lifetime of the object artificially
  54. set beresp.ttl = 300s;
  55. set beresp.grace = 300s;
  56. set beresp.http.X-Cacheable = "YES:Forced";
  57. } else {
  58. // Varnish determined the object was cacheable
  59. set beresp.http.X-Cacheable = "YES";
  60. }
  61. return(deliver);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement