Advertisement
Guest User

NLP balancer Varnish configuration

a guest
Jan 14th, 2020
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. # systemd service (with multiple ports for testing)
  2.  
  3. [Unit]
  4. Description=Varnish HTTP accelerator
  5. Documentation=https://www.varnish-cache.org/docs/6.1/ man:varnishd
  6.  
  7. [Service]
  8. Type=simple
  9. Restart=always
  10. LimitNOFILE=131072
  11. LimitMEMLOCK=82000
  12. ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -a :8000 -a :8001 -a :8002 -a :8003 -a :8004 -a :8005 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,14g
  13. ExecReload=/usr/share/varnish/varnishreload
  14. ProtectSystem=full
  15. ProtectHome=true
  16. PrivateTmp=true
  17. PrivateDevices=true
  18.  
  19. [Install]
  20. WantedBy=multi-user.target
  21.  
  22.  
  23.  
  24.  
  25. # Varnish VCL configuration
  26.  
  27. vcl 4.0;
  28.  
  29. import directors; # load the directors
  30.  
  31. # Default backend definition. Set this to point to your content server.
  32. backend nlp1 {
  33. .host = "nlp1";
  34. .host_header = "nlp1";
  35. .probe = {
  36. .url = "/status";
  37. .timeout = 1s;
  38. .interval = 5s;
  39. .window = 5;
  40. .threshold = 3;
  41. }
  42. }
  43.  
  44. backend nlp2 {
  45. .host = "nlp2";
  46. .host_header = "nlp2";
  47. .probe = {
  48. .url = "/status";
  49. .timeout = 1s;
  50. .interval = 5s;
  51. .window = 5;
  52. .threshold = 3;
  53. }
  54. }
  55.  
  56. backend nlp3 {
  57. .host = "nlp3";
  58. .host_header = "nlp3";
  59. .probe = {
  60. .url = "/status";
  61. .timeout = 1s;
  62. .interval = 5s;
  63. .window = 5;
  64. .threshold = 3;
  65. }
  66. }
  67.  
  68. backend nlp4 {
  69. .host = "nlp4";
  70. .host_header = "nlp4";
  71. .probe = {
  72. .url = "/status";
  73. .timeout = 1s;
  74. .interval = 5s;
  75. .window = 5;
  76. .threshold = 3;
  77. }
  78. }
  79.  
  80. backend nlp5 {
  81. .host = "nlp5";
  82. .host_header = "nlp5";
  83. .probe = {
  84. .url = "/status";
  85. .timeout = 1s;
  86. .interval = 5s;
  87. .window = 5;
  88. .threshold = 3;
  89. }
  90. }
  91.  
  92. backend nlp6 {
  93. .host = "nlp6";
  94. .host_header = "nlp6";
  95. .probe = {
  96. .url = "/status";
  97. .timeout = 1s;
  98. .interval = 5s;
  99. .window = 5;
  100. .threshold = 3;
  101. }
  102. }
  103.  
  104. sub vcl_init {
  105. new nlp_vdir = directors.round_robin();
  106. nlp_vdir.add_backend(nlp1);
  107. nlp_vdir.add_backend(nlp2);
  108. nlp_vdir.add_backend(nlp3);
  109. nlp_vdir.add_backend(nlp4);
  110. nlp_vdir.add_backend(nlp5);
  111. nlp_vdir.add_backend(nlp6);
  112. }
  113.  
  114. sub vcl_recv {
  115. # Happens before we check if we have this in cache already.
  116. #
  117. # Typically you clean up the request here, removing cookies you don't need,
  118. # rewriting the request, etc.
  119.  
  120. # Set backend
  121. set req.backend_hint = nlp_vdir.backend();
  122.  
  123. # Pass if X-No-Cache is set
  124. if (req.http.X-No-Cache){
  125. return(pass);
  126. }
  127.  
  128. # Unset cookies
  129. unset req.http.Cookie;
  130. }
  131.  
  132. sub vcl_backend_fetch {
  133. # Use host defined in backend
  134. unset bereq.http.host;
  135.  
  136. return(fetch);
  137. }
  138.  
  139. sub vcl_backend_response {
  140. # Happens after we have read the response headers from the backend.
  141. #
  142. # Here you clean the response headers, removing silly Set-Cookie headers
  143. # and other mistakes your backend does.
  144.  
  145. unset beresp.http.Set-Cookie;
  146.  
  147. // Set TTL if request that should be cached and status code is 2xx, 3xx or 4xx
  148. if (
  149. beresp.status >= 200 && beresp.status < 500
  150. && beresp.http.Cache-Control !~ "no-cache"
  151. ){
  152. set beresp.ttl = 1w;
  153. }
  154. else {
  155. set beresp.ttl = 0s;
  156. }
  157. }
  158.  
  159. sub vcl_deliver {
  160. # Happens when we have all the pieces we need, and are about to send the
  161. # response to the client.
  162. #
  163. # You can do accounting or modifying the final object here.
  164.  
  165. if (obj.hits > 0){
  166. set resp.http.X-Cache = "HIT";
  167. }
  168. else {
  169. set resp.http.X-Cache = "MISS";
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement