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

252 lines
8.0 KiB

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