Guest User

livefree_default.vcl

a guest
Jan 4th, 2013
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. # We only have one backend to define: NGINX
  2. backend default {
  3. .host = "127.0.0.1";
  4. .port = "8080";
  5. }
  6.  
  7. # Only allow purging from specific IPs
  8. acl purge {
  9. "localhost";
  10. "127.0.0.1";
  11. }
  12.  
  13. sub vcl_recv {
  14. # Handle compression correctly. Different browsers send different
  15. # "Accept-Encoding" headers, even though they mostly support the same
  16. # compression mechanisms. By consolidating compression headers into
  17. # a consistent format, we reduce the cache size and get more hits.
  18. # @see: http:// varnish.projects.linpro.no/wiki/FAQ/Compression
  19. if (req.http.Accept-Encoding) {
  20. if (req.http.Accept-Encoding ~ "gzip") {
  21. # If the browser supports it, we'll use gzip.
  22. set req.http.Accept-Encoding = "gzip";
  23. }
  24. else if (req.http.Accept-Encoding ~ "deflate") {
  25. # Next, try deflate if it is supported.
  26. set req.http.Accept-Encoding = "deflate";
  27. }
  28. else {
  29. # Unknown algorithm. Remove it and send unencoded.
  30. unset req.http.Accept-Encoding;
  31. }
  32. }
  33.  
  34. # Set client IP
  35. if (req.http.x-forwarded-for) {
  36. set req.http.X-Forwarded-For =
  37. req.http.X-Forwarded-For + ", " + client.ip;
  38. } else {
  39. set req.http.X-Forwarded-For = client.ip;
  40. }
  41.  
  42. # Check if we may purge (only localhost)
  43. if (req.request == "PURGE") {
  44. if (!client.ip ~ purge) {
  45. error 405 "Not allowed.";
  46. }
  47. return(lookup);
  48. }
  49.  
  50. if (req.request != "GET" &&
  51. req.request != "HEAD" &&
  52. req.request != "PUT" &&
  53. req.request != "POST" &&
  54. req.request != "TRACE" &&
  55. req.request != "OPTIONS" &&
  56. req.request != "DELETE") {
  57. # /* Non-RFC2616 or CONNECT which is weird. */
  58. return (pipe);
  59. }
  60.  
  61. if (req.request != "GET" && req.request != "HEAD") {
  62. # /* We only deal with GET and HEAD by default */
  63. return (pass);
  64. }
  65.  
  66. # admin users always miss the cache add this before to just enable backend no-cache: req.url ~ "^/wp-(login|admin)" &&
  67. if(
  68. req.http.Cookie ~ "wordpress_logged_in_" ){
  69. return (pass);
  70. }
  71.  
  72. # Remove cookies set by Google Analytics (pattern: '__utmABC')
  73. if (req.http.Cookie) {
  74. set req.http.Cookie = regsuball(req.http.Cookie,
  75. "(^|; ) *__utm.=[^;]+;? *", "\1");
  76. if (req.http.Cookie == "") {
  77. remove req.http.Cookie;
  78. }
  79. }
  80.  
  81. # always pass through POST requests and those with basic auth
  82. if (req.http.Authorization && req.request == "POST") {
  83. return (pass);
  84. }
  85.  
  86. # Do not cache these paths
  87. if (req.url ~ "^/wp-cron\.php$" ||
  88. req.url ~ "^/xmlrpc\.php$" ||
  89. req.url ~ "^/wp-admin/.*$" ||
  90. req.url ~ "^/wp-includes/.*$" ||
  91. req.url ~ "\?s=" ||
  92. req.url ~ ".*fbconnect.*" ||
  93. req.url ~ ".*facebook.*" ||
  94. req.url ~ ".*fblink.*"){
  95. return (pass);
  96. }
  97.  
  98. # Define the default grace period to serve cached content
  99. set req.grace = 30s;
  100.  
  101. # By ignoring any other cookies, it is now ok to get a page
  102. unset req.http.Cookie;
  103. return (lookup);
  104. }
  105.  
  106. sub vcl_fetch {
  107. # remove some headers we never want to see
  108. unset beresp.http.Server;
  109. unset beresp.http.X-Powered-By;
  110.  
  111. # Do not cache these paths
  112. if (req.url ~ "wp-(login|admin)" ||
  113. req.url ~ "\?s=" ||
  114. req.url ~ "xmlrpc.php") {
  115. return (hit_for_pass);
  116. }
  117.  
  118. # only allow cookies to be set if we're in admin area
  119. if( beresp.http.Set-Cookie && req.url !~ "^/wp-(login|admin)" ){
  120. unset beresp.http.Set-Cookie;
  121. }
  122.  
  123. # don't cache response to posted requests or those with basic auth
  124. if ( req.request == "POST" && req.http.Authorization ) {
  125. return (hit_for_pass);
  126. }
  127.  
  128. # only cache status ok
  129. if ( beresp.status != 200 ) {
  130. return (hit_for_pass);
  131. }
  132.  
  133. # If our backend returns 5xx status this will reset the grace time
  134. # set in vcl_recv so that cached content will be served and
  135. # the unhealthy backend will not be hammered by requests
  136. if (beresp.status == 500) {
  137. set beresp.grace = 60s;
  138. return (restart);
  139. }
  140.  
  141. # GZip the cached content if possible
  142. if (beresp.http.content-type ~ "text") {
  143. set beresp.do_gzip = true;
  144. }
  145.  
  146. # if nothing abovce matched it is now ok to cache the response
  147. set beresp.ttl = 24h;
  148. return (deliver);
  149. }
  150.  
  151. sub vcl_deliver {
  152. # remove some headers added by varnish
  153. unset resp.http.Via;
  154. unset resp.http.X-Varnish;
  155. }
  156.  
  157. sub vcl_hit {
  158. # Set up invalidation of the cache so purging gets done properly
  159. if (req.request == "PURGE") {
  160. purge;
  161. error 200 "Purged.";
  162. }
  163. return (deliver);
  164. }
  165.  
  166. sub vcl_miss {
  167. # Set up invalidation of the cache so purging gets done properly
  168. if (req.request == "PURGE") {
  169. purge;
  170. error 200 "Purged.";
  171. }
  172. return (fetch);
  173. }
  174.  
  175. sub vcl_error {
  176. if (obj.status == 503) {
  177. # set obj.http.location = req.http.Location;
  178. set obj.status = 404;
  179. set obj.response = "Not Found";
  180. return (deliver);
  181. }
  182. }
Add Comment
Please, Sign In to add comment