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.

122 lines
3.4KB

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