Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

152 рядки
3.9KB

  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. # Don't cache objects that require authentication
  53. if (beresp.http.Authorization && !beresp.http.Cache-Control ~ "public") {
  54. set beresp.uncacheable = true;
  55. return (deliver);
  56. }
  57. # Allow serving cached content for 6h in case backend goes down
  58. set beresp.grace = 6h;
  59. # Do not cache 5xx responses
  60. if (beresp.status == 500 || beresp.status == 502 || beresp.status == 503 || beresp.status == 504) {
  61. set beresp.uncacheable = true;
  62. return (abandon);
  63. }
  64. # Do not cache redirects and errors
  65. if ((beresp.status >= 300) && (beresp.status < 500)) {
  66. set beresp.uncacheable = true;
  67. set beresp.ttl = 30s;
  68. return (deliver);
  69. }
  70. }
  71. # The synthetic response for 301 redirects
  72. sub vcl_synth {
  73. if (resp.status == 750) {
  74. set resp.status = 301;
  75. set resp.http.Location = req.http.x-redir;
  76. return(deliver);
  77. }
  78. }
  79. # Ensure WebSockets through the pipe do not close prematurely
  80. sub vcl_pipe {
  81. if (req.http.upgrade) {
  82. set bereq.http.upgrade = req.http.upgrade;
  83. set bereq.http.connection = req.http.connection;
  84. }
  85. }
  86. sub vcl_hash {
  87. # CHUNKED SUPPORT
  88. if (req.http.x-range ~ "bytes=") {
  89. hash_data(req.http.x-range);
  90. unset req.http.Range;
  91. }
  92. }
  93. sub vcl_backend_fetch {
  94. # Be more lenient for slow servers on the fediverse
  95. if bereq.url ~ "^/proxy/" {
  96. set bereq.first_byte_timeout = 300s;
  97. }
  98. # CHUNKED SUPPORT
  99. if (bereq.http.x-range) {
  100. set bereq.http.Range = bereq.http.x-range;
  101. }
  102. if (bereq.retries == 0) {
  103. # Clean up the X-Varnish-Backend-503 flag that is used internally
  104. # to mark broken backend responses that should be retried.
  105. unset bereq.http.X-Varnish-Backend-503;
  106. } else {
  107. if (bereq.http.X-Varnish-Backend-503) {
  108. if (bereq.method != "POST" &&
  109. std.healthy(bereq.backend) &&
  110. bereq.retries <= 4) {
  111. # Flush broken backend response flag & try again.
  112. unset bereq.http.X-Varnish-Backend-503;
  113. } else {
  114. return (abandon);
  115. }
  116. }
  117. }
  118. }
  119. sub vcl_deliver {
  120. # CHUNKED SUPPORT
  121. if (resp.http.CR) {
  122. set resp.http.Content-Range = resp.http.CR;
  123. unset resp.http.CR;
  124. }
  125. }
  126. sub vcl_backend_error {
  127. # Retry broken backend responses.
  128. set bereq.http.X-Varnish-Backend-503 = "1";
  129. return (retry);
  130. }