varnish session - 不同的session,不同的缓存。 - varnish

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;
}













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值