Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

253 lignes
8.0 KiB

  1. vcl 4.0;
  2. import std;
  3. # The minimal Varnish version is 4.0
  4. # For SSL offloading, pass the following header in your proxy server or load balancer: '/* {{ ssl_offloaded_header }} */: https'
  5. backend default {
  6. .host = "/* {{ host }} */";
  7. .port = "/* {{ port }} */";
  8. .first_byte_timeout = 600s;
  9. .probe = {
  10. .url = "/pub/health_check.php";
  11. .timeout = 2s;
  12. .interval = 5s;
  13. .window = 10;
  14. .threshold = 5;
  15. }
  16. }
  17. acl purge {
  18. /* {{ ips }} */
  19. }
  20. sub vcl_recv {
  21. if (req.method == "PURGE") {
  22. if (client.ip !~ purge) {
  23. return (synth(405, "Method not allowed"));
  24. }
  25. # To use the X-Pool header for purging varnish during automated deployments, make sure the X-Pool header
  26. # has been added to the response in your backend server config. This is used, for example, by the
  27. # capistrano-magento2 gem for purging old content from varnish during it's deploy routine.
  28. if (!req.http.X-Magento-Tags-Pattern && !req.http.X-Pool) {
  29. return (synth(400, "X-Magento-Tags-Pattern or X-Pool header required"));
  30. }
  31. if (req.http.X-Magento-Tags-Pattern) {
  32. ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
  33. }
  34. if (req.http.X-Pool) {
  35. ban("obj.http.X-Pool ~ " + req.http.X-Pool);
  36. }
  37. return (synth(200, "Purged"));
  38. }
  39. if (req.method != "GET" &&
  40. req.method != "HEAD" &&
  41. req.method != "PUT" &&
  42. req.method != "POST" &&
  43. req.method != "TRACE" &&
  44. req.method != "OPTIONS" &&
  45. req.method != "DELETE") {
  46. /* Non-RFC2616 or CONNECT which is weird. */
  47. return (pipe);
  48. }
  49. # We only deal with GET and HEAD by default
  50. if (req.method != "GET" && req.method != "HEAD") {
  51. return (pass);
  52. }
  53. # Bypass shopping cart and checkout
  54. if (req.url ~ "/checkout") {
  55. return (pass);
  56. }
  57. # Bypass health check requests
  58. if (req.url ~ "/pub/health_check.php") {
  59. return (pass);
  60. }
  61. # Set initial grace period usage status
  62. set req.http.grace = "none";
  63. # normalize url in case of leading HTTP scheme and domain
  64. set req.url = regsub(req.url, "^http[s]?://", "");
  65. # collect all cookies
  66. std.collect(req.http.Cookie);
  67. # Compression filter. See https://www.varnish-cache.org/trac/wiki/FAQ/Compression
  68. if (req.http.Accept-Encoding) {
  69. if (req.url ~ "\.(jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|flv)$") {
  70. # No point in compressing these
  71. unset req.http.Accept-Encoding;
  72. } elsif (req.http.Accept-Encoding ~ "gzip") {
  73. set req.http.Accept-Encoding = "gzip";
  74. } elsif (req.http.Accept-Encoding ~ "deflate" && req.http.user-agent !~ "MSIE") {
  75. set req.http.Accept-Encoding = "deflate";
  76. } else {
  77. # unknown algorithm
  78. unset req.http.Accept-Encoding;
  79. }
  80. }
  81. # Remove all marketing get parameters to minimize the cache objects
  82. if (req.url ~ "(\?|&)(gclid|cx|ie|cof|siteurl|zanpid|origin|fbclid|mc_[a-z]+|utm_[a-z]+|_bta_[a-z]+)=") {
  83. set req.url = regsuball(req.url, "(gclid|cx|ie|cof|siteurl|zanpid|origin|fbclid|mc_[a-z]+|utm_[a-z]+|_bta_[a-z]+)=[-_A-z0-9+()%.]+&?", "");
  84. set req.url = regsub(req.url, "[?|&]+$", "");
  85. }
  86. # Static files caching
  87. if (req.url ~ "^/(pub/)?(media|static)/") {
  88. # Static files should not be cached by default
  89. return (pass);
  90. # But if you use a few locales and don't use CDN you can enable caching static files by commenting previous line (#return (pass);) and uncommenting next 3 lines
  91. #unset req.http.Https;
  92. #unset req.http./* {{ ssl_offloaded_header }} */;
  93. #unset req.http.Cookie;
  94. }
  95. # Bypass authenticated GraphQL requests without a X-Magento-Cache-Id
  96. if (req.url ~ "/graphql" && !req.http.X-Magento-Cache-Id && req.http.Authorization ~ "^Bearer") {
  97. return (pass);
  98. }
  99. return (hash);
  100. }
  101. sub vcl_hash {
  102. if ((req.url !~ "/graphql" || !req.http.X-Magento-Cache-Id) && req.http.cookie ~ "X-Magento-Vary=") {
  103. hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1"));
  104. }
  105. if (req.url ~ "/graphql") {
  106. call process_graphql_headers;
  107. }
  108. # To make sure http users don't see ssl warning
  109. if (req.http./* {{ ssl_offloaded_header }} */) {
  110. hash_data(req.http./* {{ ssl_offloaded_header }} */);
  111. }
  112. /* {{ design_exceptions_code }} */
  113. }
  114. sub process_graphql_headers {
  115. if (req.http.X-Magento-Cache-Id) {
  116. hash_data(req.http.X-Magento-Cache-Id);
  117. # When the frontend stops sending the auth token, make sure users stop getting results cached for logged-in users
  118. if (req.http.Authorization ~ "^Bearer") {
  119. hash_data("Authorized");
  120. }
  121. }
  122. if (req.http.Store) {
  123. hash_data(req.http.Store);
  124. }
  125. if (req.http.Content-Currency) {
  126. hash_data(req.http.Content-Currency);
  127. }
  128. }
  129. sub vcl_backend_response {
  130. set beresp.grace = 3d;
  131. if (beresp.http.content-type ~ "text") {
  132. set beresp.do_esi = true;
  133. }
  134. if (bereq.url ~ "\.js$" || beresp.http.content-type ~ "text") {
  135. set beresp.do_gzip = true;
  136. }
  137. if (beresp.http.X-Magento-Debug) {
  138. set beresp.http.X-Magento-Cache-Control = beresp.http.Cache-Control;
  139. }
  140. # cache only successfully responses and 404s
  141. if (beresp.status != 200 && beresp.status != 404) {
  142. set beresp.ttl = 120s;
  143. set beresp.uncacheable = true;
  144. return (deliver);
  145. } elsif (beresp.http.Cache-Control ~ "private") {
  146. set beresp.uncacheable = true;
  147. set beresp.ttl = 86400s;
  148. return (deliver);
  149. }
  150. # validate if we need to cache it and prevent from setting cookie
  151. if (beresp.ttl > 0s && (bereq.method == "GET" || bereq.method == "HEAD")) {
  152. unset beresp.http.set-cookie;
  153. }
  154. # If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
  155. if (beresp.ttl <= 0s ||
  156. beresp.http.Surrogate-control ~ "no-store" ||
  157. (!beresp.http.Surrogate-Control &&
  158. beresp.http.Cache-Control ~ "no-cache|no-store") ||
  159. beresp.http.Vary == "*") {
  160. # Mark as Hit-For-Pass for the next 2 minutes
  161. set beresp.ttl = 120s;
  162. set beresp.uncacheable = true;
  163. }
  164. # If the cache key in the Magento response doesn't match the one that was sent in the request, don't cache under the request's key
  165. if (bereq.url ~ "/graphql" && bereq.http.X-Magento-Cache-Id && bereq.http.X-Magento-Cache-Id != beresp.http.X-Magento-Cache-Id) {
  166. set beresp.ttl = 0s;
  167. set beresp.uncacheable = true;
  168. }
  169. return (deliver);
  170. }
  171. sub vcl_deliver {
  172. if (resp.http.x-varnish ~ " ") {
  173. set resp.http.X-Magento-Cache-Debug = "HIT";
  174. set resp.http.Grace = req.http.grace;
  175. } else {
  176. set resp.http.X-Magento-Cache-Debug = "MISS";
  177. }
  178. # Not letting browser to cache non-static files.
  179. if (resp.http.Cache-Control !~ "private" && req.url !~ "^/(pub/)?(media|static)/") {
  180. set resp.http.Pragma = "no-cache";
  181. set resp.http.Expires = "-1";
  182. set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
  183. }
  184. if (!resp.http.X-Magento-Debug) {
  185. unset resp.http.Age;
  186. }
  187. unset resp.http.X-Magento-Debug;
  188. unset resp.http.X-Magento-Tags;
  189. unset resp.http.X-Powered-By;
  190. unset resp.http.Server;
  191. unset resp.http.X-Varnish;
  192. unset resp.http.Via;
  193. unset resp.http.Link;
  194. }
  195. sub vcl_hit {
  196. if (obj.ttl >= 0s) {
  197. # Hit within TTL period
  198. return (deliver);
  199. }
  200. if (std.healthy(req.backend_hint)) {
  201. if (obj.ttl + /* {{ grace_period }} */s > 0s) {
  202. # Hit after TTL expiration, but within grace period
  203. set req.http.grace = "normal (healthy server)";
  204. return (deliver);
  205. } else {
  206. # Hit after TTL and grace expiration
  207. return (fetch);
  208. }
  209. } else {
  210. # server is not healthy, retrieve from cache
  211. set req.http.grace = "unlimited (unhealthy server)";
  212. return (deliver);
  213. }
  214. }