Advertisement
Guest User

varnish

a guest
Aug 16th, 2013
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. backend default {
  2. .host = "127.0.0.1";
  3. .port = "81";
  4. }
  5.  
  6. sub vcl_recv {
  7. set req.backend = default;
  8. ######XAV#####
  9. if (req.url ~ "\.(png|gif|jpg|jpeg|js|css|ico|pdf)$")
  10. {
  11. unset req.http.Cookie;
  12. #return (lookup);
  13. }
  14.  
  15. ######/XAV####
  16. if (req.restarts == 0)
  17. {
  18. if (req.http.x-forwarded-for)
  19. {
  20. set req.http.X-Forwarded-For =
  21. req.http.X-Forwarded-For + ", " + client.ip;
  22. }
  23. else
  24. {
  25. set req.http.X-Forwarded-For = client.ip;
  26. }
  27. }
  28. if (req.request != "GET" &&
  29. req.request != "HEAD" &&
  30. req.request != "PUT" &&
  31. req.request != "POST" &&
  32. req.request != "TRACE" &&
  33. req.request != "OPTIONS" &&
  34. req.request != "DELETE") {
  35. /* Non-RFC2616 or CONNECT which is weird. */
  36. return (pipe);
  37. }
  38. if (req.request != "GET" && req.request != "HEAD") {
  39. /* We only deal with GET and HEAD by default */
  40. return (pass);
  41. }
  42. if (req.http.Authorization || req.http.Cookie) {
  43. /* Not cacheable by default */
  44. return (pass);
  45. }
  46. return (lookup);
  47. }
  48.  
  49. sub vcl_pipe {
  50. # Note that only the first request to the backend will have
  51. # X-Forwarded-For set. If you use X-Forwarded-For and want to
  52. # have it set for all requests, make sure to have:
  53. # set bereq.http.connection = "close";
  54. # here. It is not set by default as it might break some broken web
  55. # applications, like IIS with NTLM authentication.
  56. return (pipe);
  57. }
  58.  
  59. sub vcl_pass {
  60. return (pass);
  61. }
  62.  
  63. sub vcl_hash {
  64. hash_data(req.url);
  65. if (req.http.host) {
  66. hash_data(req.http.host);
  67. } else {
  68. hash_data(server.ip);
  69. }
  70. return (hash);
  71. }
  72.  
  73. sub vcl_hit {
  74. return (deliver);
  75. }
  76.  
  77. sub vcl_miss {
  78. return (fetch);
  79. }
  80.  
  81. sub vcl_fetch {
  82. # Varnish determined the object was not cacheable
  83. if (beresp.ttl <= 0s) {
  84. set beresp.http.X-Cacheable = "NO:Not Cacheable";
  85.  
  86. # You don't wish to cache content for logged in users
  87. } elsif (req.http.Cookie ~ "(UserID|_session)") {
  88. set beresp.http.X-Cacheable = "NO:Got Session";
  89. return(hit_for_pass);
  90.  
  91. # You are respecting the Cache-Control=private header from the backend
  92. } elsif (beresp.http.Cache-Control ~ "private") {
  93. set beresp.http.X-Cacheable = "NO:Cache-Control=private";
  94. return(hit_for_pass);
  95.  
  96. # Varnish determined the object was cacheable
  97. } else {
  98. set beresp.http.X-Cacheable = "YES";
  99. }
  100.  
  101. if (beresp.ttl <= 0s ||
  102. beresp.http.Set-Cookie ||
  103. beresp.http.Vary == "*") {
  104. /*
  105. * Mark as "Hit-For-Pass" for the next 2 minutes
  106. */
  107.  
  108. set beresp.ttl = 120 s;
  109. return (hit_for_pass);
  110. }
  111. return (deliver);
  112. }
  113.  
  114. sub vcl_deliver {
  115. if (obj.hits > 0)
  116. {
  117. set resp.http.X-Cache = "HIT";
  118. set resp.http.X-Cache-Hits = obj.hits;
  119. # set resp.http.X-Cache-Backend = req.backend;
  120. }
  121. else
  122. {
  123. set resp.http.X-Cache = "MISS";
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130. #remove resp.http.X-Varnish;
  131. remove resp.http.Via;
  132. remove resp.http.Age;
  133. remove resp.http.X-Powered-By;
  134.  
  135. return (deliver);
  136. }
  137.  
  138. sub vcl_error {
  139. set obj.http.Content-Type = "text/html; charset=utf-8";
  140. set obj.http.Retry-After = "5";
  141. synthetic {"
  142. <?xml version="1.0" encoding="utf-8"?>
  143. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  144. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  145. <html>
  146. <head>
  147. <title>"} + obj.status + " " + obj.response + {"</title>
  148. </head>
  149. <body>
  150. <h1>Error "} + obj.status + " " + obj.response + {"</h1>
  151. <p>"} + obj.response + {"</p>
  152. <h3>Guru Meditation:</h3>
  153. <p>XID: "} + req.xid + {"</p>
  154. <hr>
  155. <p>Varnish cache server</p>
  156. </body>
  157. </html>
  158. "};
  159. return (deliver);
  160. }
  161.  
  162. sub vcl_init {
  163. return (ok);
  164. }
  165.  
  166. sub vcl_fini {
  167. return (ok);
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement