logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

pleroma.vcl (3552B)


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