Advertisement
Guest User

Varnish VCL Client IP issue

a guest
Apr 5th, 2012
1,699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.98 KB | None | 0 0
  1. # Default backend definition. Set this to point to your content
  2. # server.
  3. backend mysite {
  4. .host = "www.mysite.com";
  5. .port = "8085";
  6. .connect_timeout = 600s;
  7. .first_byte_timeout = 600s;
  8. .between_bytes_timeout = 600s;
  9. }
  10.  
  11. acl purge {
  12. # For now, I'll only allow purges coming from localhost
  13. "127.0.0.1";
  14. "localhost";
  15. }
  16.  
  17. # Handle the HTTP request received by the client
  18. sub vcl_recv {
  19. # if (req.restarts == 0) {
  20. # if (req.http.X-Forwarded-For) {
  21. # set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
  22. # } else {
  23. # set req.http.X-Forwarded-For = client.ip;
  24. # }
  25. # }
  26. # unset req.http.X-Forwarded-For;
  27.  
  28. # Add a unique header containing the client address
  29. # remove req.http.X-Forwarded-For;
  30. # set req.http.X-Forwarded-For = client.ip;
  31. #
  32. # Rename the incoming XFF header to work around a Varnish bug.
  33. # if (req.http.X-Forwarded-For) {
  34. # Append the client IP
  35. # // set req.http.X-Real-Forwarded-For = req.http.X-Forwarded-For ", " regsub(client.ip, ":.*", "");
  36. # unset req.http.X-Forwarded-For;
  37. # }
  38. # else {
  39. // Simply use the client IP
  40. // set req.http.X-Real-Forwarded-For = regsub(client.ip, ":.*", "");
  41. # }
  42.  
  43.  
  44. #lize the header, remove the port (in case you're testing this on various TCP ports)
  45. set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");
  46.  
  47. # Allow purging
  48. if (req.request == "PURGE") {
  49. if (!client.ip ~ purge) {
  50. # Not from an allowed IP? Then die with an error.
  51. error 405 "This IP is not allowed to send PURGE requests.";
  52. }
  53.  
  54. # If you got this stage (and didn't error out above), do a cache-lookup
  55. # That will force entry into vcl_hit() or vcl_miss() below and purge the actual cache
  56. return (lookup);
  57. }
  58.  
  59. # Only deal with "normal" types
  60. if (req.request != "GET" &&
  61. req.request != "HEAD" &&
  62. req.request != "PUT" &&
  63. req.request != "POST" &&
  64. req.request != "TRACE" &&
  65. req.request != "OPTIONS" &&
  66. req.request != "DELETE") {
  67. /* Non-RFC2616 or CONNECT which is weird. */
  68. return (pipe);
  69. }
  70.  
  71. if (req.request != "GET" && req.request != "HEAD") {
  72. # We only deal with GET and HEAD by default
  73. return (pass);
  74. }
  75.  
  76. # mysite.com - With any subdomain support
  77. if (req.http.host ~ "^(.*\.)?mysite\.com$") {
  78. set req.backend = mysite;
  79. //return (pass); // Means never cache anything from this domain
  80. }
  81.  
  82. # A configuration file specific for Drupal 7
  83.  
  84. # Either the admin pages or the login
  85. if (req.url ~ "/admin/?") {
  86. # Don't cache, pass to backend
  87. return (pass);
  88. }
  89.  
  90. if (req.url ~ "/user?") {
  91. # don't cache
  92. return (pass);
  93. }
  94.  
  95. #if (req.http.Cookie ~ "(FITNESS_LOGGED)") {
  96. # return (pass);
  97. #}
  98.  
  99. # Remove the "has_js" cookie
  100. set req.http.Cookie = regsuball(req.http.Cookie, "has_js=[^;]+(; )?", "");
  101.  
  102. # Remove any Google Analytics based cookies
  103. set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(; )?", "");
  104.  
  105. # Remove the Quant Capital cookies (added by some plugin, all __qca)
  106. set req.http.Cookie = regsuball(req.http.Cookie, "__qc.=[^;]+(; )?", "");
  107.  
  108. # Are there cookies left with only spaces or that are empty?
  109. if (req.http.cookie ~ "^ *$") {
  110. unset req.http.cookie;
  111. }
  112.  
  113. # Static content unique to the theme can be cached (so no user uploaded images)
  114. if (req.url ~ "^/themes/" && req.url ~ "\.(css|js|png|gif|jp(e)?g)") {
  115. unset req.http.cookie;
  116. }
  117.  
  118. # Normalize Accept-Encoding header (straight from the manual: https://www.varnish-cache.org/docs/3.0/tutorial/vary.html)
  119. if (req.http.Accept-Encoding) {
  120. if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
  121. # No point in compressing these
  122. remove req.http.Accept-Encoding;
  123. } elsif (req.http.Accept-Encoding ~ "gzip") {
  124. set req.http.Accept-Encoding = "gzip";
  125. } elsif (req.http.Accept-Encoding ~ "deflate") {
  126. set req.http.Accept-Encoding = "deflate";
  127. } else {
  128. # unkown algorithm
  129. remove req.http.Accept-Encoding;
  130. }
  131. }
  132.  
  133. # Don't cache the install, update or cron files in Drupal
  134. if (req.url ~ "install\.php|update\.php|cron\.php|members/") {
  135. return (pass);
  136. }
  137.  
  138. # Uncomment this to trigger the vcl_error() subroutine, which will HTML output you some variables (HTTP 700 = pretty debug)
  139. #error 700;
  140.  
  141. # Anything else left?
  142. if (!req.http.cookie) {
  143. unset req.http.cookie;
  144. }
  145.  
  146. // Rename the incoming XFF header to work around a Varnish bug.
  147. # if (req.http.X-Forwarded-For) {
  148. // Append the client IP
  149. #set req.http.X-Real-Forwarded-For = req.http.X-Forwarded-For ", " regsub(client.ip, ":.*", "");
  150. # unset req.http.X-Forwarded-For;
  151. # }
  152. # else {
  153. // Simply use the client IP
  154. #set req.http.X-Real-Forwarded-For = regsub(client.ip, ":.*", "");
  155. # }
  156.  
  157. set req.http.X-Forwarded-For = regsub(client.ip, ":.*", "");
  158. set req.http.X-Real-Forwarded-For = regsub(client.ip, ":.*", "");
  159.  
  160. # Try a cache-lookup
  161. # return (lookup);
  162.  
  163. if (req.http.Authorization || req.http.Cookie) {
  164. # Not cacheable by default
  165. return (pass);
  166. }
  167.  
  168. return (lookup);
  169. }
  170.  
  171. sub vcl_pipe {
  172. # Note that only the first request to the backend will have
  173. # X-Forwarded-For set. If you use X-Forwarded-For and want to
  174. # have it set for all requests, make sure to have:
  175. # set bereq.http.connection = "close";
  176. # here. It is not set by default as it might break some broken web
  177. # applications, like IIS with NTLM authentication.
  178.  
  179. set bereq.http.connection = "close";
  180. return (pipe);
  181. }
  182.  
  183. sub vcl_pass {
  184. return (pass);
  185. }
  186.  
  187. # The data on which the hashing will take place
  188. sub vcl_hash {
  189. hash_data(req.url);
  190. if (req.http.host) {
  191. hash_data(req.http.host);
  192. } else {
  193. hash_data(server.ip);
  194. }
  195.  
  196. # If the client supports compression, keep that in a different cache
  197. if (req.http.Accept-Encoding) {
  198. hash_data(req.http.Accept-Encoding);
  199. }
  200.  
  201. return (hash);
  202. }
  203.  
  204. sub vcl_hit {
  205. # Allow purges
  206. if (req.request == "PURGE") {
  207. purge;
  208. error 200 "Purged.";
  209. }
  210.  
  211. return (deliver);
  212. }
  213.  
  214. sub vcl_miss {
  215. # Allow purges
  216. if (req.request == "PURGE") {
  217. purge;
  218. error 200 "Purged.";
  219. }
  220.  
  221. return (fetch);
  222. }
  223.  
  224. # Handle the HTTP request coming from our backend
  225. sub vcl_fetch {
  226. # I can use direct matching on the host, since I normalized the host header in the VCL Receive
  227. if (req.http.host ~ "^(.*\.)?fitnessrepublic\.com$") {
  228.  
  229. # The vcl_fetch routine, when the request is fetched from the backend
  230.  
  231. # For static content related to the theme, strip all backend cookies
  232. if (req.url ~ "^/themes/" && req.url ~ "\.(css|js|png|gif|jp(e?)g)") {
  233. unset beresp.http.cookie;
  234. }
  235.  
  236. # A TTL of 30 minutes
  237. set beresp.ttl = 1800s;
  238. }
  239.  
  240. # Temporarily removed
  241. #if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary == "*") {
  242. # set beresp.ttl = 120s;
  243. # return (hit_for_pass);
  244. #}
  245. return (deliver);
  246. }
  247.  
  248. # The routine when we deliver the HTTP request to the user
  249. # Last chance to modify headers that are sent to the client
  250. sub vcl_deliver {
  251. if (obj.hits > 0) {
  252. set resp.http.X-Cache = "cached";
  253. } else {
  254. set resp.http.x-Cache = "uncached";
  255. }
  256.  
  257. # Remove some headers: PHP version
  258. unset resp.http.X-Powered-By;
  259.  
  260. # Remove some headers: Apache version & OS
  261. unset resp.http.Server;
  262.  
  263. return (deliver);
  264. }
  265.  
  266. sub vcl_error {
  267. if (obj.status == 700) {
  268. # Include a general error message handler for debugging purposes
  269. include "/etc/varnish/conf.d/_error.vcl";
  270.  
  271. } elseif (obj.status == 701) {
  272. # Redirect error handler
  273. set obj.http.Location = "http://" + obj.response + req.url;
  274. # Change this to 302 if you want temporary redirects
  275. set obj.status = 301;
  276. return (deliver);
  277.  
  278. }
  279.  
  280. return (deliver);
  281. }
  282.  
  283. sub vcl_init {
  284. return (ok);
  285. }
  286.  
  287. sub vcl_fini {
  288. return (ok);
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement