Guest User

VCL

a guest
Aug 30th, 2023
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1.  
  2. vcl 4.0;
  3.  
  4. # Default backend definition. Set this to point to your content server.
  5. backend api {
  6. .host = "api-nginx";
  7. .port = "8086";
  8. }
  9.  
  10. //backend client {
  11. // .host = "node-client";
  12. // .port = "3000";
  13. //}
  14.  
  15. # list IPs allowed to purge/ban
  16. acl invalidators {
  17. "localhost";
  18. // "varnish";
  19. "php";
  20. "127.0.0.1";
  21. }
  22.  
  23. # before checking if the cache exists
  24. sub vcl_recv {
  25. // if (req.http.Host ~ "client.localhost") {
  26. // set req.backend_hint = client;
  27. // } else {
  28. // set req.backend_hint = api;
  29. // }
  30.  
  31. if (req.url ~ "^/admin/.*" || req.url ~ "^/build/.*" || req.url ~ "(\?|&)cache=false\b") {
  32. return (pass);
  33. }
  34.  
  35. if (req.method == "BAN") {
  36. if (client.ip !~ invalidators) {
  37. return (synth(405, "Not allowed"));
  38. }
  39.  
  40. if (req.http.ApiPlatform-Ban-Regex) {
  41. ban("obj.http.Cache-Tags ~ " + req.http.ApiPlatform-Ban-Regex);
  42.  
  43. return (synth(200, "Ban added"));
  44. }
  45.  
  46. return (synth(400, "ApiPlatform-Ban-Regex HTTP header must be set."));
  47. }
  48.  
  49. if (req.method == "PRI") {
  50. /* This will never happen in properly formed traffic (see: RFC7540) */
  51. return (synth(405));
  52. }
  53. if (!req.http.host &&
  54. req.esi_level == 0 &&
  55. req.proto ~ "^(?i)HTTP/1.1") {
  56. /* In HTTP/1.1, Host is required. */
  57. return (synth(400));
  58. }
  59. if (req.method != "GET" &&
  60. req.method != "HEAD" &&
  61. req.method != "PUT" &&
  62. req.method != "POST" &&
  63. req.method != "TRACE" &&
  64. req.method != "OPTIONS" &&
  65. req.method != "DELETE" &&
  66. req.method != "PATCH") {
  67. /* Non-RFC2616 or CONNECT which is weird. */
  68. return (pipe);
  69. }
  70.  
  71. if (req.method != "GET" && req.method != "HEAD") {
  72. /* We only deal with GET and HEAD by default */
  73. return (pass);
  74. }
  75.  
  76. unset req.http.forwarded;
  77.  
  78. if (
  79. req.url ~ "(\?|&)cache=true\b" ||
  80. req.url ~ "^\/api\/activities" ||
  81. req.url ~ "^\/api\/activity_types" ||
  82. req.url ~ "^\/api\/attributes(?!\/revisions)" ||
  83. req.url ~ "^\/api\/awards" ||
  84. req.url ~ "^\/api\/comments\/get_by_point_of_interest" ||
  85. req.url ~ "^\/api\/comments\/last_community" ||
  86. req.url ~ "^\/api\/configs" ||
  87. req.url ~ "^\/api\/currency_types" ||
  88. req.url ~ "^\/api\/destinations" ||
  89. req.url ~ "^\/api\/detail_person_views" ||
  90. req.url ~ "^\/api\/directory_countries" ||
  91. req.url ~ "^\/api\/directory_departments" ||
  92. req.url ~ "^\/api\/elastic_activities" ||
  93. req.url ~ "^\/api\/known_types.*" ||
  94. req.url ~ "^\/api\/locales" ||
  95. req.url ~ "^\/api\/main_revisions" ||
  96. req.url ~ "^\/api\/media.*" ||
  97. req.url ~ "^\/api\/metadata\/seo" ||
  98. req.url ~ "^\/api\/partners.*" ||
  99. req.url ~ "^\/api\/payment_types" ||
  100. req.url ~ "^\/api\/people\/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}\/rank" ||
  101. req.url ~ "^\/api\/person_stat_views\/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}" ||
  102. req.url ~ "^\/api\/pictures(?!\/)" ||
  103. req.url ~ "^\/api\/point_of_interest_comments" ||
  104. req.url ~ "^\/api\/point_of_interest_types" ||
  105. req.url ~ "^\/api\/point_of_interests\/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}\/comments" ||
  106. req.url ~ "^\/api\/point_of_interests\/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}\/count_comments" ||
  107. req.url ~ "^\/api\/point_of_interests\/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}\/main_revision" ||
  108. req.url ~ "^\/api\/press.*" ||
  109. req.url ~ "^\/api\/revisions(?!\/main_with_pictures).*" ||
  110. req.url ~ "^\/api\/team_members.*" ||
  111. req.url ~ "^\/api\/translations\/.*" ||
  112. req.url ~ "^\/api\/trip_categories" ||
  113. req.url ~ "^\/api\/trip_countries" ||
  114. req.url ~ "^\/api\/trips(?!\/)"
  115. ) {
  116. unset req.http.Cookie;
  117. unset req.http.Authorization;
  118. } else {
  119. return (pass);
  120. }
  121. }
  122.  
  123. sub vcl_hash {
  124. if(req.http.accept-language) {
  125. hash_data(req.url + ";__lang__=" + req.http.accept-language);
  126. } else {
  127. hash_data(req.url + ";__lang__=" + "fr");
  128. }
  129. return (lookup);
  130. }
  131.  
  132. # after reading the res headers from the back
  133. sub vcl_backend_response {
  134. if (bereq.uncacheable) {
  135. return (deliver);
  136. }
  137.  
  138. if (beresp.status == 200 || beresp.status == 201) {
  139. set beresp.ttl = 1d;
  140. }
  141.  
  142. set beresp.http.url = bereq.url;
  143. }
  144.  
  145. # before sending res to client
  146. sub vcl_deliver {
  147. # Don't send cache tags related headers to the client
  148. unset resp.http.url;
  149. # Comment the following line to send the "Cache-Tags" header to the client (e.g. to use CloudFlare cache tags)
  150. unset resp.http.Cache-Tags;
  151.  
  152. unset resp.http.X-Powered-By;
  153. unset resp.http.Server;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment