default_wordpress.vcl 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. vcl 4.1;
  2. # Based on: https://github.com/mattiasgeniar/varnish-6.0-configuration-templates/blob/master/default.vcl
  3. import std;
  4. import directors;
  5. backend everpracticalsolutionsServer { # Define one backend
  6. .host = "${BACKEND_HOST}"; # IP or Hostname of backend
  7. .port = "${BACKEND_PORT}"; # Port Apache or whatever is listening
  8. .max_connections = 300; # That's it
  9. .probe = {
  10. #.url = "/"; # short easy way (GET /)
  11. # We prefer to only do a HEAD /
  12. .request =
  13. "HEAD /health_check.php HTTP/1.1"
  14. "Host: ${BACKEND_HOST}"
  15. "Connection: close"
  16. "User-Agent: Varnish Health Probe";
  17. .interval = 5s; # check the health of each backend every 5 seconds
  18. .timeout = 1s; # timing out after 1 second.
  19. .window = 5; # If 3 out of the last 5 polls succeeded the backend is considered healthy, otherwise it will be marked as sick
  20. .threshold = 3;
  21. }
  22. .first_byte_timeout = 300s; # How long to wait before we receive a first byte from our backend?
  23. .connect_timeout = 5s; # How long to wait for a backend connection?
  24. .between_bytes_timeout = 2s; # How long to wait between bytes received from our backend?
  25. }
  26. # Only allow purging from specific IPs
  27. acl purge {
  28. "localhost";
  29. "127.0.0.1";
  30. "192.168.16.5";
  31. "192.168.16.6";
  32. "185.228.234.203";
  33. }
  34. # This function is used when a request is send by a HTTP client (Browser)
  35. sub vcl_recv {
  36. # Normalize the header, remove the port (in case you're testing this on various TCP ports)
  37. set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");
  38. # Allow purging from ACL
  39. if (req.method == "PURGE") {
  40. # If not allowed then a error 405 is returned
  41. if (!client.ip ~ purge) {
  42. return(synth(405, "This IP is not allowed to send PURGE requests."));
  43. }
  44. ban("req.http.host == " + req.http.host);
  45. # Throw a synthetic page so the request won't go to the backend.
  46. return(synth(200, "Ban added"));
  47. # If allowed, do a cache_lookup -> vlc_hit() or vlc_miss()
  48. #return (purge);
  49. }
  50. # Post requests will not be cached
  51. if (req.http.Authorization || req.method == "POST") {
  52. return (pass);
  53. }
  54. # --- WordPress specific configuration
  55. # Did not cache the RSS feed
  56. if (req.url ~ "/feed") {
  57. return (pass);
  58. }
  59. # Blitz hack
  60. if (req.url ~ "/mu-.*") {
  61. return (pass);
  62. }
  63. # Did not cache the admin and login pages
  64. if (req.url ~ "/wp-(login|admin)") {
  65. return (pass);
  66. }
  67. # Remove the "has_js" cookie
  68. set req.http.Cookie = regsuball(req.http.Cookie, "has_js=[^;]+(; )?", "");
  69. # Remove any Google Analytics based cookies
  70. set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(; )?", "");
  71. # Remove the Quant Capital cookies (added by some plugin, all __qca)
  72. set req.http.Cookie = regsuball(req.http.Cookie, "__qc.=[^;]+(; )?", "");
  73. # Remove the wp-settings-1 cookie
  74. set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-1=[^;]+(; )?", "");
  75. # Remove the wp-settings-time-1 cookie
  76. set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-time-1=[^;]+(; )?", "");
  77. # Remove the wp test cookie
  78. set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie=[^;]+(; )?", "");
  79. # Are there cookies left with only spaces or that are empty?
  80. if (req.http.cookie ~ "^ *$") {
  81. unset req.http.cookie;
  82. }
  83. # Cache the following files extensions
  84. if (req.url ~ "\.(css|js|png|gif|jp(e)?g|swf|ico)") {
  85. unset req.http.cookie;
  86. }
  87. # Normalize Accept-Encoding header and compression
  88. # https://www.varnish-cache.org/docs/3.0/tutorial/vary.html
  89. if (req.http.Accept-Encoding) {
  90. # Do no compress compressed files...
  91. if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
  92. unset req.http.Accept-Encoding;
  93. } elsif (req.http.Accept-Encoding ~ "gzip") {
  94. set req.http.Accept-Encoding = "gzip";
  95. } elsif (req.http.Accept-Encoding ~ "deflate") {
  96. set req.http.Accept-Encoding = "deflate";
  97. } else {
  98. unset req.http.Accept-Encoding;
  99. }
  100. }
  101. # Check the cookies for wordpress-specific items
  102. if (req.http.Cookie ~ "wordpress_" || req.http.Cookie ~ "comment_") {
  103. return (pass);
  104. }
  105. if (!req.http.cookie) {
  106. unset req.http.cookie;
  107. }
  108. # --- End of WordPress specific configuration
  109. # Do not cache HTTP authentication and HTTP Cookie
  110. if (req.http.Authorization || req.http.Cookie) {
  111. # Not cacheable by default
  112. return (pass);
  113. }
  114. # Cache all others requests
  115. return (hash);
  116. }
  117. sub vcl_pipe {
  118. return (pipe);
  119. }
  120. sub vcl_pass {
  121. return (fetch);
  122. }
  123. # The data on which the hashing will take place
  124. sub vcl_hash {
  125. hash_data(req.url);
  126. if (req.http.host) {
  127. hash_data(req.http.host);
  128. } else {
  129. hash_data(server.ip);
  130. }
  131. # If the client supports compression, keep that in a different cache
  132. if (req.http.Accept-Encoding) {
  133. hash_data(req.http.Accept-Encoding);
  134. }
  135. return (lookup);
  136. }
  137. # This function is used when a request is sent by our backend (Nginx server)
  138. sub vcl_backend_response {
  139. # Remove some headers we never want to see
  140. unset beresp.http.Server;
  141. unset beresp.http.X-Powered-By;
  142. # For static content strip all backend cookies
  143. if (bereq.url ~ "\.(css|js|png|gif|jp(e?)g)|swf|ico") {
  144. unset beresp.http.cookie;
  145. }
  146. # Only allow cookies to be set if we're in admin area
  147. if (beresp.http.Set-Cookie && bereq.url !~ "^/wp-(login|admin)") {
  148. unset beresp.http.Set-Cookie;
  149. }
  150. # don't cache response to posted requests or those with basic auth
  151. if ( bereq.method == "POST" || bereq.http.Authorization ) {
  152. set beresp.uncacheable = true;
  153. set beresp.ttl = 120s;
  154. return (deliver);
  155. }
  156. # don't cache search results
  157. if ( bereq.url ~ "\?s=" ){
  158. set beresp.uncacheable = true;
  159. set beresp.ttl = 120s;
  160. return (deliver);
  161. }
  162. # only cache status ok
  163. if ( beresp.status != 200 ) {
  164. set beresp.uncacheable = true;
  165. set beresp.ttl = 120s;
  166. return (deliver);
  167. }
  168. # A TTL of 24h
  169. set beresp.ttl = 24h;
  170. # Define the default grace period to serve cached content
  171. set beresp.grace = 30s;
  172. return (deliver);
  173. }
  174. # The routine when we deliver the HTTP request to the user
  175. # Last chance to modify headers that are sent to the client
  176. sub vcl_deliver {
  177. if (obj.hits > 0) {
  178. set resp.http.X-Cache = "cached";
  179. } else {
  180. set resp.http.x-Cache = "uncached";
  181. }
  182. # Remove some headers: PHP version
  183. unset resp.http.X-Powered-By;
  184. # Remove some headers: Apache version & OS
  185. unset resp.http.Server;
  186. # Remove some heanders: Varnish
  187. unset resp.http.Via;
  188. unset resp.http.X-Varnish;
  189. return (deliver);
  190. }
  191. sub vcl_init {
  192. return (ok);
  193. }
  194. sub vcl_fini {
  195. return (ok);
  196. }