Advertisement
Guest User

default.vcl

a guest
Jul 7th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.98 KB | None | 0 0
  1. vcl 4.0;
  2. import std;
  3.  
  4. # localhost isn't my IP, i use NAT IP at .host
  5. backend default {
  6. .host = "localhost";
  7. .port = "8080";
  8. .connect_timeout = 600s;
  9. .first_byte_timeout = 600s;
  10. .between_bytes_timeout = 600s;
  11. .max_connections = 800;
  12. }
  13.  
  14.  
  15. # SET THE ALLOWED IP OF PURGE REQUESTS
  16. acl purge {
  17. "localhost";
  18. "127.0.0.1";
  19. "here my NAT IP";
  20. }
  21.  
  22. #THE RECV FUNCTION
  23. sub vcl_recv {
  24. # Normalize the header, remove the port (in case you're testing this on various TCP ports)
  25. set req.http.X-Actual-IP = regsub(req.http.X-Forwarded-For, "[, ].*$", "");
  26.  
  27. # FORWARD THE IP OF THE REQUEST
  28. if (req.restarts == 0) {
  29. if (req.http.x-forwarded-for) {
  30. set req.http.X-Forwarded-For =
  31. req.http.X-Forwarded-For + ", " + client.ip;
  32. } else {
  33. set req.http.X-Forwarded-For = client.ip;
  34. }
  35. }
  36.  
  37. # Enable smart refreshing using hash_always_miss
  38. if (req.http.Cache-Control ~ "no-cache") {
  39. if (client.ip ~ purge || std.ip(req.http.X-Actual-IP, "here my NAT IP") ~ purge) {
  40. set req.hash_always_miss = true;
  41. }
  42. }
  43.  
  44. if (req.method == "PURGE") {
  45. if (!client.ip ~ purge || !std.ip(req.http.X-Actual-IP, "here my NAT IP") ~ purge) {
  46. return(synth(405,"Not allowed."));
  47. }
  48. return (purge);
  49. }
  50.  
  51. if (req.method == "BAN") {
  52. # Same ACL check as above:
  53. if (!client.ip ~ purge || !std.ip(req.http.X-Actual-IP, "here my NAT IP") ~ purge) {
  54. return(synth(403, "Not allowed."));
  55. }
  56. ban("req.http.host == " + req.http.host +
  57. " && req.url == " + req.url);
  58.  
  59. # Throw a synthetic page so the
  60. # request won't go to the backend.
  61. return(synth(200, "Ban added"));
  62. }
  63.  
  64. # Unset cloudflare cookies
  65. # Remove has_js and CloudFlare/Google Analytics __* cookies.
  66. set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(_[_a-z]+|has_js)=[^;]*", "");
  67.  
  68. # Remove a ";" prefix, if present.
  69.  
  70. set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
  71.  
  72. # For Testing: If you want to test with Varnish passing (not caching) uncomment
  73. # return( pass );
  74.  
  75. # CONFIGURACAO PARA WORDPRESS #
  76. # Rss
  77. if (req.url ~ "/feed(/)?") {
  78. return ( pass );
  79. }
  80. #Pass wp-cron
  81. if (req.url ~ "wp-cron\.php.*") {
  82. return ( pass );
  83. }
  84. ## Do not cache search results, comment these 3 lines if you do want to cache them
  85. if (req.url ~ "/\?s\=") {
  86. return ( pass );
  87. }
  88.  
  89.  
  90. #################################
  91.  
  92.  
  93. # SET TO GZIP, DEFLATE, OR REMOVE ENTIRELY. WITH VARY ACCEPT-ENCODING
  94. # VARNISH WILL CREATE SEPARATE CACHES FOR EACH
  95. if (req.http.Accept-Encoding) {
  96. if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
  97. # No point in compressing these
  98. unset req.http.Accept-Encoding;
  99. } elsif (req.http.Accept-Encoding ~ "gzip") {
  100. set req.http.Accept-Encoding = "gzip";
  101. } elsif (req.http.Accept-Encoding ~ "deflate") {
  102. set req.http.Accept-Encoding = "deflate";
  103. } else {
  104. # unknown algorithm
  105. unset req.http.Accept-Encoding;
  106. }
  107. }
  108.  
  109. # PIPE ALL NON-STANDARD REQUESTS
  110. if (req.method != "GET" &&
  111. req.method != "HEAD" &&
  112. req.method != "PUT" &&
  113. req.method != "POST" &&
  114. req.method != "TRACE" &&
  115. req.method != "OPTIONS" &&
  116. req.method != "DELETE") {
  117. return (pipe);
  118. }
  119.  
  120. # ONLY CACHE GET AND HEAD REQUESTS
  121. if (req.method != "GET" && req.method != "HEAD") {
  122. return (pass);
  123. }
  124.  
  125. # OPTIONAL: DO NOT CACHE LOGGED IN USERS (THIS OCCURS IN FETCH TOO, EITHER
  126. if ( req.http.cookie ~ "wordpress_logged_in" ) {
  127. return( pass );
  128. }
  129.  
  130. #IF THE REQUEST IS NOT FOR A PREVIEW, WP-ADMIN OR WP-LOGIN THEN UNSET THE COOKIES
  131. if (req.url ~ "wp-(login|admin)" || req.url ~ "preview=true") {
  132. return (pass);
  133. }
  134.  
  135. if (!(req.url ~ "wp-(login|admin)")){
  136. unset req.http.cookie;
  137. }
  138.  
  139. if ( !( req.url ~ "wp-(login|admin)" )) {
  140. unset req.http.Cookie;
  141. }
  142.  
  143. # IF BASIC AUTH IS ON THEN DO NOT CACHE
  144. if (req.http.Authorization || req.http.Cookie) {
  145. return (pass);
  146. }
  147.  
  148. ###################################################
  149.  
  150. # Post requests will not be cached
  151. if (req.http.Authorization || req.method == "POST") {
  152. return (pass);
  153. }
  154.  
  155. # --- Wordpress specific configuration
  156.  
  157. # Blitz hack
  158. if (req.url ~ "/mu-.*") {
  159. return (pass);
  160. }
  161.  
  162.  
  163. # Did not cache the admin and login pages
  164. if (req.url ~ "wp-(login|admin)" || req.url ~ "preview=true") {
  165. return (pass);
  166. }
  167.  
  168. # Do not cache the WooCommerce pages
  169. ### REMOVE IT IF YOU DO NOT USE WOOCOMMERCE ###
  170. if (req.url ~ "/(cart|my-account|checkout|addons|/?add-to-cart=)") {
  171. return (pass);
  172. }
  173.  
  174. # Remove the "has_js" cookie
  175. set req.http.Cookie = regsuball(req.http.Cookie, "has_js=[^;]+(; )?", "");
  176.  
  177. # Remove any Google Analytics based cookies
  178. set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(; )?", "");
  179.  
  180. # Remove the Quant Capital cookies (added by some plugin, all __qca)
  181. set req.http.Cookie = regsuball(req.http.Cookie, "__qc.=[^;]+(; )?", "");
  182.  
  183. # Remove the wp-settings-1 cookie
  184. set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-1=[^;]+(; )?", "");
  185.  
  186. # Remove the wp-settings-time-1 cookie
  187. set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-time-1=[^;]+(; )?", "");
  188.  
  189. # Remove the wp test cookie
  190. set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie=[^;]+(; )?", "");
  191.  
  192. # Are there cookies left with only spaces or that are empty?
  193. if (req.http.cookie ~ "^ *$") {
  194. unset req.http.cookie;
  195. }
  196.  
  197. # Cache the following files extensions
  198. if (req.url ~ "\.(css|js|png|gif|jp(e)?g|swf|ico)") {
  199. unset req.http.cookie;
  200. }
  201.  
  202. # Normalize Accept-Encoding header and compression
  203. # https://www.varnish-cache.org/docs/3.0/tutorial/vary.html
  204. if (req.http.Accept-Encoding) {
  205. # Do no compress compressed files...
  206. if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
  207. unset req.http.Accept-Encoding;
  208. } elsif (req.http.Accept-Encoding ~ "gzip") {
  209. set req.http.Accept-Encoding = "gzip";
  210. } elsif (req.http.Accept-Encoding ~ "deflate") {
  211. set req.http.Accept-Encoding = "deflate";
  212. } else {
  213. unset req.http.Accept-Encoding;
  214. }
  215. }
  216.  
  217. # Check the cookies for wordpress-specific items
  218. if (req.http.Cookie ~ "wordpress_" || req.http.Cookie ~ "comment_") {
  219. return (pass);
  220. }
  221. if (!req.http.cookie) {
  222. unset req.http.cookie;
  223. }
  224.  
  225. # --- End of Wordpress specific configuration
  226.  
  227. # Did not cache HTTP authentication and HTTP Cookie
  228. if (req.http.Authorization || req.http.Cookie) {
  229. # Not cacheable by default
  230. return (pass);
  231. }
  232.  
  233. # Cache all others requests
  234. return (hash);
  235. }
  236.  
  237.  
  238. # PASS FUNCTION
  239. sub vcl_pass {
  240. return (fetch);
  241. }
  242.  
  243. # HIT FUNCTION
  244. sub vcl_hit {
  245. return (deliver);
  246. }
  247.  
  248. # MISS FUNCTION
  249. sub vcl_miss {
  250. return (fetch);
  251. }
  252.  
  253. # FETCH FUNCTION
  254. sub vcl_backend_response {
  255. # I SET THE VARY TO ACCEPT-ENCODING, THIS OVERRIDES W3TC
  256. # TENDANCY TO SET VARY USER-AGENT. YOU MAY OR MAY NOT WANT
  257. # TO DO THIS
  258. set beresp.http.Vary = "Accept-Encoding";
  259.  
  260. # IF NOT WP-ADMIN THEN UNSET COOKIES AND SET THE AMOUNT OF
  261. # TIME THIS PAGE WILL STAY CACHED (TTL)
  262. if (!(bereq.url ~ "(wp-login|wp-admin|preview=true)") && !bereq.http.cookie ~ "wordpress_logged_in" ) {
  263. unset beresp.http.set-cookie;
  264. set beresp.ttl = 52w;
  265. # set beresp.grace =1d;
  266. }
  267.  
  268. if (beresp.ttl <= 0s ||
  269. beresp.http.Set-Cookie ||
  270. beresp.http.Vary == "*") {
  271. set beresp.ttl = 120 s;
  272. # set beresp.ttl = 120s;
  273. set beresp.uncacheable = true;
  274. return (deliver);
  275. }
  276.  
  277. return (deliver);
  278. }
  279.  
  280. # DELIVER FUNCTION
  281. sub vcl_deliver {
  282. if (obj.hits > 0) {
  283. set resp.http.X-Cache = "HIT";
  284. } else {
  285. set resp.http.X-Cache = "MISS";
  286. }
  287. set resp.http.X-Cache-Hits = obj.hits;
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement