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.

159 lines
4.1KB

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