1.参考资料:
sub vcl_hash {
### these 2 entries are the default ones used for vcl. Below we add our own.
set req.hash += req.url;
set req.hash += req.http.host;
### Hash differently based on a cookie value. For example, some users may
### want "not safe for work" content as part of their search results, and
### some users may not.
if( req.url ~ '^/search' && req.http.Cookie ~ "show_nsfw=1" ) {
### add this fact to the hash
set req.hash += "show nsfw";
### set a header so it can be logged in the backend/varnishlog
set req.http.X-Show-NSFW = "1";
}
### regsub replaces only the bit in your match criteria with whatever you
### ask it too. In this case, we need to remove *EVERYTHING* else from the
### cookie, except the UUID, hence the full string match
### This is using the UUID, which will always stay the same. If you wish
### to cache based on the session, you could use "myapp_session" instead.
### The regex is PCRE, so it works only in trunk. If you wish to use it
### with Varnish 2.0, remove the '?' non-greedy operator and be aware it
### changes the behavior of the regex.
if( req.http.Cookie ~ "myapp_unique_user_id" ) {
set req.http.X-Varnish-Hashed-On =
regsub( req.http.Cookie, "^.*?myapp_unique_user_id=([^;]*);*.*$", "\1" );
}
### if the esi request is UUID specific, add the UUID to the hashing
### The only requirement on the application is now that UUID specific
### content mentions the string UUID in it.
if( req.url ~ "/esi/.*UUID" && req.http.X-Varnish-Hashed-On ) {
set req.hash += req.http.X-Varnish-Hashed-On;
}
### if the esi request is LOGIN specific, add a string to the hashing
### The only requirement on the application is now that LOGIN specific
### content mentions the string LOGIN in it.
if( req.url ~ "/esi/.*LOGIN" && req.http.X-Varnish-Hashed-On ) {
set req.hash += "logged in";
}
hash;
}
2.
php code:
$value = 'access';
setcookie("auth_user", $value);
setcookie("auth_user", $value, time()+86400);
setcookie("auth_user", $value, time()+86400, "/", ".mysite.com", 1);
/etc/varnish/default.vcl:
sub vcl_recv {
if (req.http.cookie && req.http.cookie ~ "auth_user") {
pass;
}
}
sub vcl_hash {
if (req.http.cookie && req.http.cookie ~ "auth_user") { set req.hash +=
"auth"; }
set req.hash += req.url;
hash;
}