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.8KB

  1. vcl 4.0;
  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. # Pleroma MediaProxy - strip headers that will affect caching
  33. if (req.url ~ "^/proxy/") {
  34. unset req.http.Cookie;
  35. unset req.http.Authorization;
  36. unset req.http.Accept;
  37. return (hash);
  38. }
  39. # Strip headers that will affect caching from all other static content
  40. # This also permits caching of individual toots and AP Activities
  41. if ((req.url ~ "^/(media|static)/") ||
  42. (req.url ~ "(?i)\.(html|js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|mp4|ogg|webm|svg|swf|ttf|pdf|woff|woff2)$"))
  43. {
  44. unset req.http.Cookie;
  45. unset req.http.Authorization;
  46. return (hash);
  47. }
  48. }
  49. sub vcl_backend_response {
  50. # gzip text content
  51. if (beresp.http.content-type ~ "(text|text/css|application/x-javascript|application/javascript)") {
  52. set beresp.do_gzip = true;
  53. }
  54. # CHUNKED SUPPORT
  55. if (bereq.http.x-range ~ "bytes=" && beresp.status == 206) {
  56. set beresp.ttl = 10m;
  57. set beresp.http.CR = beresp.http.content-range;
  58. }
  59. # Don't cache objects that require authentication
  60. if (beresp.http.Authorization && !beresp.http.Cache-Control ~ "public") {
  61. set beresp.uncacheable = true;
  62. return (deliver);
  63. }
  64. # Default object caching of 86400s;
  65. set beresp.ttl = 86400s;
  66. # Allow serving cached content for 6h in case backend goes down
  67. set beresp.grace = 6h;
  68. # Do not cache 5xx responses
  69. if (beresp.status == 500 || beresp.status == 502 || beresp.status == 503 || beresp.status == 504) {
  70. set beresp.uncacheable = true;
  71. return (abandon);
  72. }
  73. # Do not cache redirects and errors
  74. if ((beresp.status >= 300) && (beresp.status < 500)) {
  75. set beresp.uncacheable = true;
  76. set beresp.ttl = 30s;
  77. return (deliver);
  78. }
  79. # Pleroma MediaProxy internally sets headers properly
  80. if (bereq.url ~ "^/proxy/") {
  81. return (deliver);
  82. }
  83. # Strip cache-restricting headers from Pleroma on static content that we want to cache
  84. if (bereq.url ~ "(?i)\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|mp4|ogg|webm|svg|swf|ttf|pdf|woff|woff2)$")
  85. {
  86. unset beresp.http.set-cookie;
  87. unset beresp.http.Cache-Control;
  88. unset beresp.http.x-request-id;
  89. set beresp.http.Cache-Control = "public, max-age=86400";
  90. }
  91. }
  92. # The synthetic response for 301 redirects
  93. sub vcl_synth {
  94. if (resp.status == 750) {
  95. set resp.status = 301;
  96. set resp.http.Location = req.http.x-redir;
  97. return(deliver);
  98. }
  99. }
  100. # Ensure WebSockets through the pipe do not close prematurely
  101. sub vcl_pipe {
  102. if (req.http.upgrade) {
  103. set bereq.http.upgrade = req.http.upgrade;
  104. set bereq.http.connection = req.http.connection;
  105. }
  106. }
  107. sub vcl_hash {
  108. # CHUNKED SUPPORT
  109. if (req.http.x-range ~ "bytes=") {
  110. hash_data(req.http.x-range);
  111. unset req.http.Range;
  112. }
  113. }
  114. sub vcl_backend_fetch {
  115. # CHUNKED SUPPORT
  116. if (bereq.http.x-range) {
  117. set bereq.http.Range = bereq.http.x-range;
  118. }
  119. }
  120. sub vcl_deliver {
  121. # CHUNKED SUPPORT
  122. if (resp.http.CR) {
  123. set resp.http.Content-Range = resp.http.CR;
  124. unset resp.http.CR;
  125. }
  126. }