Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- backend images_gluster1 {
- .host = "192.168.0.50";
- .port = "82";
- .connect_timeout = 15s;
- .first_byte_timeout = 60s;
- .between_bytes_timeout = 15s;
- # Backend polling: http://varnish.projects.linpro.no/wiki/BackendPolling
- .probe = {
- .url = "/polling/tour_search.png";
- .timeout = 10s;
- .interval = 1m;
- .window = 3;
- .threshold = 1;
- }
- }
- backend resources_gluster1 {
- .host = "192.168.0.50";
- .port = "83";
- .connect_timeout = 15s;
- .first_byte_timeout = 60s;
- .between_bytes_timeout = 15s;
- # Backend polling: http://varnish.projects.linpro.no/wiki/BackendPolling
- .probe = {
- .url = "/polling/tour_search.png";
- .timeout = 10s;
- .interval = 1m;
- .window = 3;
- .threshold = 1;
- }
- }
- sub vcl_init {
- # define directors and add backends
- # resources
- new res = directors.round_robin();
- res.add_backend(resources_gluster1);
- res.add_backend(resources_gluster2);
- res.add_backend(resources_gluster3);
- # images
- new img = directors.round_robin();
- img.add_backend(images_gluster1);
- img.add_backend(images_gluster2);
- img.add_backend(images_gluster3);
- }
- sub vcl_hash {
- hash_data(req.url);
- hash_data(req.http.host);
- if (req.url ~ "^/api/" && req.http.Accept ~ "json" ) {
- hash_data(req.http.Accept);
- }
- return(lookup);
- }
- sub vcl_hit {
- # Called when a cache lookup is successful.
- if (obj.ttl >= 0s) {
- # A pure unadultered hit, deliver it
- return (deliver);
- }
- if (std.healthy(req.backend_hint)) {
- # Backend is healthy. Limit age to 30s.
- if (obj.ttl + 30s > 0s) {
- #set req.http.grace = "normal(limited)";
- return (deliver);
- } else {
- # No candidate for grace. Fetch a fresh object.
- return(miss);
- }
- } else {
- # backend is sick - use full grace
- if (obj.ttl + obj.grace > 0s) {
- #set req.http.grace = "full";
- return (deliver);
- } else {
- # no graced object.
- return (miss);
- }
- }
- # fetch & deliver once we get the result
- return (miss); # Dead code, keep as a safeguard
- }
- sub vcl_miss {
- # Called after a cache lookup if the requested document was not found in the cache. Its purpose
- # is to decide whether or not to attempt to retrieve the document from the backend, and which
- # backend to use.
- return (fetch);
- }
- sub vcl_backend_response {
- # Happens after we have read the response headers from the backend.
- #
- # Here you clean the response headers, removing silly Set-Cookie headers
- # and other mistakes your backend does.
- # Set server header
- # set req.grace = 3h;
- unset beresp.http.Server;
- set beresp.http.Server = "Apache/2 (CBM-64)";
- # Remove E-Tags from all replies except from json
- if (! (bereq.url ~ "\.json" || bereq.url ~ "\.jsonp")) {
- unset beresp.http.Etag;
- }
- # Don't cache 404 responses
- if (beresp.status == 404) {
- set beresp.ttl = 10s;
- set beresp.uncacheable = true;
- return (deliver);
- }
- else if (beresp.status<300) {
- set beresp.http.X-Varnish-Comment = "Caching rule applied";
- if (bereq.url ~ "^/img/" || bereq.url ~ "^/img2/" ) {
- unset bereq.http.Cookie;
- unset beresp.http.Set-Cookie;
- set beresp.ttl = 31d;
- set beresp.http.Cache-Control = "max-age=2678400";
- return(deliver);
- }
- # If no cookie set: Use default caching time
- if (!bereq.http.Cookie) {
- set beresp.ttl = 15m;
- set beresp.http.Cache-Control = "max-age=900";
- return(deliver);
- } else {
- set beresp.http.X-Varnish-Comment = "Logged in";
- set beresp.uncacheable = true;
- return(deliver);
- }
- } else {
- # Use very short caching time for error messages - giving the system the chance to recover
- set beresp.ttl = 120s;
- unset beresp.http.Cache-Control;
- return(deliver);
- }
- }
- sub vcl_deliver {
- # Happens when we have all the pieces we need, and are about to send the
- # response to the client.
- #
- # You can do accounting or modifying the final object here.
- if (obj.hits > 0) {
- set resp.http.X-Cache = "HIT";
- } else {
- set resp.http.X-Cache = "MISS";
- }
- set resp.http.X-Cache-Hits = obj.hits;
- return(deliver);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement