The method of deploying varnish Cache Server in centos Environment
Press "F5" to refresh:
The header information specified in our configuration file is accessed, and the status code is 304.
Verify the ACL clear cache configuration:
Clear the cache on host 192.168.171.132 (varnish is configured not to allow this IP to clear the cache):
[root@localhost ~] # curl-X "PURGE" 192.168.171.135 # clear the cache of varnish
You will get the following error message:
If you clear the cache on the IP allowed by varnish (192.168.171.134 host), you will see the following successful message:
Attach:
The above complete uncommented configuration file is as follows:
Vcl 4.0 backend web1 import directors;import std;probe backend_healthcheck {.url = "/"; .interval = 5spoli.timeout = 1spoli.window = 5; .threshold = 3;} backend web1 {.host = "192.168.171.134"; .port = "80"; .probe = backend_healthcheck;} backend web2 {.url = "192.168.171.132"; .port = "80"; .port = backend_healthcheck } acl purgers {"127.0.0.1"; "localhost"; "192.168.171.0 web2 24";! "192.168.171.132";} sub vcl_init {new web_cluster=directors.round_robin (); web_cluster.add_backend (web1); web_cluster.add_backend (web2) } sub vcl_recv {set req.backend_hint = web_cluster.backend (); if (req.method = = "PURGE") {if (! client.ip ~ purgers) {return (synth (405, "Not Allowed."));} return (purge) } if (req.method! = "GET" & & req.method! = "HEAD" & & req.method! = "PUT" & & req.method! = "POST" & & req.method! = "TRACE" & & req.method! = "OPTIONS" & & req.method! = "PATCH" & & req.method! = "DELETE") { Return (pipe) } if (req.method! = "GET" & & req.method! = "HEAD") {return (pass);} if (req.url ~ "\. (php | asp | aspx | do | ashx | shtml) ($|\?) {return (pass);} if (req.http.Authorization) {return (pass) } if (req.http.Accept-Encoding) {if (req.url ~ "\. (bmp | png | gif | jpg | jpeg | ico | gz | tgz | bz2 | tbz | zip | rar | mp3 | mp4 | ogg | swf | flv) $") {unset req.http.Accept-Encoding;} elseif (req.http.Accept-Encoding ~ "gzip") {set req.http.Accept-Encoding = "gzip" } elseif (req.http.Accept-Encoding ~ "deflate") {set req.http.Accept-Encoding = "deflate";} else {unset req.http.Accept-Encoding;}} if (req.url ~ "\. (css | js | html | htm | bmp | png | gif | jpeg | ico | gz | tgz | bz2 | tbz | zip | rar | mp3 | mp4 | ogg | swf | flv) ($|\)) {unset req.http.cookie; return (hash) } if (req.restarts = = 0) {if (req.http.X-Forwarded-For) {set req.http.X-Forwarded-For = req.http.X-Forwarded-For + "," + client.ip;} else {set req.http.X-Forwarded-For = client.ip;}} return (hash);} sub vcl_hash {hash_data (req.url) If (req.http.host) {hash_data (req.http.host);} else {hash_data (server.ip);} return (lookup);} sub vcl_hit {if (req.method = = "PURGE") {return (synth (200, "Purged.")) } return (deliver);} sub vcl_miss {if (req.method = = "PURGE") {return (synth (404, "Purged."));} return (fetch);} sub vcl_deliver {if (obj.hits > 0) {set resp.http.CXK = "HIT-from-varnish" Set resp.http.X-Cache-Hits = obj.hits;} else {set resp.http.X-Cache = "MISS";} unset resp.http.X-Powered-By; unset resp.http.Server; unset resp.http.X-Drupal-Cache; unset resp.http.Via; unset resp.http.Link; unset resp.http.X-Varnish Set resp.http.xx_restarts_count = req.restarts; set resp.http.xx_Age = resp.http.Age; # set resp.http.hit_count = obj.hits; # unset resp.http.Age; return (deliver);} sub vcl_pass {return (fetch);} sub vcl_backend_response {set beresp.grace = 5m If (beresp.status = = 499 | | beresp.status = = 404 | | beresp.status = = 502) {set beresp.uncacheable = true;} if (bereq.url ~ "\. (php | jsp) (\? | $)") {set beresp.uncacheable = true } else {if (bereq.url ~ "\. (css | js | html | htm | bmp | png | gif | jpg | jpeg | ico) ($|\?) {set beresp.ttl = 15m; unset beresp.http.Set-Cookie;} elseif (bereq.url ~"\. (gz | tgz | bz2 | tbz | zip | rar | mp3 | mp4 | ogg | swf | flv) ($|\?) {set beresp.ttl = 30m Unset beresp.http.Set-Cookie;} else {set beresp.ttl = 10m; unset beresp.http.Set-Cookie;}} return (deliver);} sub vcl_purge {return (synth (200m, "success")) } sub vcl_backend_error {if (beresp.status = = 500 | | beresp.status = = 501 | | beresp.status = = 502 | | beresp.status = = 503 | | beresp.status = = 504) {return (retry);}} sub vcl_fini {return (ok);}
If you want to implement the caching function of varnish, you can do it through the following basic definitions (the following can be found in the example.vcl file):
Vcl 4.0 backend web1 import directors;probe backend_healthcheck {.url = "/"; .timeout = 1s; .interval = 5s; .window = 5; .threshold = 3;} backend web1 {.host = "192.168.171.134"; .port = "80"; .probe = backend_healthcheck;} import {.host = "192.168.171.132" .port = "80"; .probe = backend_healthcheck;} sub vcl_init {new web_cluster = directors.round_robin (); web_cluster.add_backend (web1); web_cluster.add_backend (web2);} sub vcl_recv {set req.backend_hint = web_cluster.backend ();}
After reading this article, have you learned how to deploy a varnish cache server in a centos environment? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel.