Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.6KB

  1. vcl 4.1;
  2. import std;
  3. backend default {
  4. .host = "127.0.0.1";
  5. .port = "4000";
  6. }
  7. # ACL for IPs that are allowed to PURGE data from the cache
  8. acl purge {
  9. "127.0.0.1";
  10. }
  11. sub vcl_recv {
  12. # Redirect HTTP to HTTPS
  13. if (std.port(server.ip) != 443) {
  14. set req.http.x-redir = "https://" + req.http.host + req.url;
  15. return (synth(750, ""));
  16. }
  17. # CHUNKED SUPPORT
  18. if (req.http.Range ~ "bytes=") {
  19. set req.http.x-range = req.http.Range;
  20. }
  21. # Pipe if WebSockets request is coming through
  22. if (req.http.upgrade ~ "(?i)websocket") {
  23. return (pipe);
  24. }
  25. # Allow purging of the cache
  26. if (req.method == "PURGE") {
  27. if (!client.ip ~ purge) {
  28. return(synth(405,"Not allowed."));
  29. }
  30. return(purge);
  31. }
  32. }
  33. sub vcl_backend_response {
  34. # gzip text content
  35. if (beresp.http.content-type ~ "(text|text/css|application/x-javascript|application/javascript)") {
  36. set beresp.do_gzip = true;
  37. }
  38. # Retry broken backend responses.
  39. if (beresp.status == 503) {
  40. set bereq.http.X-Varnish-Backend-503 = "1";
  41. return (retry);
  42. }
  43. # CHUNKED SUPPORT
  44. if (bereq.http.x-range ~ "bytes=" && beresp.status == 206) {
  45. set beresp.ttl = 10m;
  46. set beresp.http.CR = beresp.http.content-range;
  47. }
  48. # Don't cache objects that require authentication
  49. if (beresp.http.Authorization && !beresp.http.Cache-Control ~ "public") {
  50. set beresp.uncacheable = true;
  51. return (deliver);
  52. }
  53. # Allow serving cached content for 6h in case backend goes down
  54. set beresp.grace = 6h;
  55. # Do not cache 5xx responses
  56. if (beresp.status == 500 || beresp.status == 502 || beresp.status == 503 || beresp.status == 504) {
  57. set beresp.uncacheable = true;
  58. return (abandon);
  59. }
  60. # Do not cache redirects and errors
  61. if ((beresp.status >= 300) && (beresp.status < 500)) {
  62. set beresp.uncacheable = true;
  63. set beresp.ttl = 30s;
  64. return (deliver);
  65. }
  66. }
  67. # The synthetic response for 301 redirects
  68. sub vcl_synth {
  69. if (resp.status == 750) {
  70. set resp.status = 301;
  71. set resp.http.Location = req.http.x-redir;
  72. return(deliver);
  73. }
  74. }
  75. # Ensure WebSockets through the pipe do not close prematurely
  76. sub vcl_pipe {
  77. if (req.http.upgrade) {
  78. set bereq.http.upgrade = req.http.upgrade;
  79. set bereq.http.connection = req.http.connection;
  80. }
  81. }
  82. sub vcl_hash {
  83. # CHUNKED SUPPORT
  84. if (req.http.x-range ~ "bytes=") {
  85. hash_data(req.http.x-range);
  86. unset req.http.Range;
  87. }
  88. }
  89. sub vcl_backend_fetch {
  90. # Be more lenient for slow servers on the fediverse
  91. if bereq.url ~ "^/proxy/" {
  92. set bereq.first_byte_timeout = 300s;
  93. }
  94. # CHUNKED SUPPORT
  95. if (bereq.http.x-range) {
  96. set bereq.http.Range = bereq.http.x-range;
  97. }
  98. if (bereq.retries == 0) {
  99. # Clean up the X-Varnish-Backend-503 flag that is used internally
  100. # to mark broken backend responses that should be retried.
  101. unset bereq.http.X-Varnish-Backend-503;
  102. } else {
  103. if (bereq.http.X-Varnish-Backend-503) {
  104. if (bereq.method != "POST" &&
  105. std.healthy(bereq.backend) &&
  106. bereq.retries <= 4) {
  107. # Flush broken backend response flag & try again.
  108. unset bereq.http.X-Varnish-Backend-503;
  109. } else {
  110. return (abandon);
  111. }
  112. }
  113. }
  114. }
  115. sub vcl_deliver {
  116. # CHUNKED SUPPORT
  117. if (resp.http.CR) {
  118. set resp.http.Content-Range = resp.http.CR;
  119. unset resp.http.CR;
  120. }
  121. }
  122. sub vcl_backend_error {
  123. # Retry broken backend responses.
  124. set bereq.http.X-Varnish-Backend-503 = "1";
  125. return (retry);
  126. }