25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

256 lines
8.2 KiB

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