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.

133 lines
3.9KB

  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. # Pipe if WebSockets request is coming through
  18. if (req.http.upgrade ~ "(?i)websocket") {
  19. return (pipe);
  20. }
  21. # Allow purging of the cache
  22. if (req.method == "PURGE") {
  23. if (!client.ip ~ purge) {
  24. return(synth(405,"Not allowed."));
  25. }
  26. return(purge);
  27. }
  28. # Pleroma MediaProxy - strip headers that will affect caching
  29. if (req.url ~ "^/proxy/") {
  30. unset req.http.Cookie;
  31. unset req.http.Authorization;
  32. unset req.http.Accept;
  33. return (hash);
  34. }
  35. # Hack to enable a Terms of Service page missing from Pleroma
  36. if (req.url ~ "^/about/more$") {
  37. set req.http.x-redir = "https://" + req.http.host + "/static/terms-of-service.html";
  38. return (synth(750, ""));
  39. }
  40. # Strip headers that will affect caching from all other static content
  41. # This also permits caching of individual toots and AP Activities
  42. if ((req.url ~ "^/(media|notice|objects|static)/") ||
  43. (req.url ~ "^/(activities/|api/v1/statuses/\d+$)") ||
  44. (req.url ~ "^/(activities/|api/v1/statuses/\d+/card$)") ||
  45. (req.url ~ "(?i)\.(html|js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|svg|swf|ttf|pdf|woff|woff2)$"))
  46. {
  47. unset req.http.Cookie;
  48. unset req.http.Authorization;
  49. return (hash);
  50. }
  51. # Everything else should just be piped to Pleroma
  52. return (pipe);
  53. }
  54. sub vcl_backend_response {
  55. # gzip text content
  56. if (beresp.http.content-type ~ "(text|text/css|application/x-javascript|application/javascript)") {
  57. set beresp.do_gzip = true;
  58. }
  59. # etags are bad
  60. unset beresp.http.etag;
  61. # Don't cache objects that require authentication
  62. if (beresp.http.Authorization && !beresp.http.Cache-Control ~ "public") {
  63. set beresp.uncacheable = true;
  64. return (deliver);
  65. }
  66. # Default object caching of 86400s;
  67. set beresp.ttl = 86400s;
  68. # Allow serving cached content for 6h in case backend goes down
  69. set beresp.grace = 6h;
  70. # Do not cache 5xx responses
  71. if (beresp.status == 500 || beresp.status == 502 || beresp.status == 503 || beresp.status == 504) {
  72. set beresp.uncacheable = true;
  73. return (abandon);
  74. }
  75. # Do not cache redirects and errors
  76. if ((beresp.status >= 300) && (beresp.status < 500)) {
  77. set beresp.uncacheable = true;
  78. set beresp.ttl = 30s;
  79. return (deliver);
  80. }
  81. # Pleroma MediaProxy internally sets headers properly
  82. if (bereq.url ~ "^/proxy/") {
  83. return (deliver);
  84. }
  85. # Strip cache-restricting headers from Pleroma on static content that we want to cache
  86. # Also enable streaming of cached content to clients (no waiting for Varnish to complete backend fetch)
  87. if ((bereq.url ~ "^/(notice|objects)/") ||
  88. (bereq.url ~ "^/(activities/|api/v1/statuses/\d+$)") ||
  89. (bereq.url ~ "^/(activities/|api/v1/statuses/\d+/card$)") ||
  90. (bereq.url ~ "(?i)\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|svg|swf|ttf|pdf|woff|woff2)$"))
  91. {
  92. unset beresp.http.set-cookie;
  93. unset beresp.http.Cache-Control;
  94. unset beresp.http.x-request-id;
  95. set beresp.http.Cache-Control = "public, max-age=86400";
  96. set beresp.do_stream = true;
  97. }
  98. }
  99. # The synthetic response for 301 redirects
  100. sub vcl_synth {
  101. if (resp.status == 750) {
  102. set resp.status = 301;
  103. set resp.http.Location = req.http.x-redir;
  104. return(deliver);
  105. }
  106. }
  107. # Ensure WebSockets through the pipe do not close prematurely
  108. sub vcl_pipe {
  109. if (req.http.upgrade) {
  110. set bereq.http.upgrade = req.http.upgrade;
  111. set bereq.http.connection = req.http.connection;
  112. }
  113. }