Mediawiki 避免在varnish中缓存来自特殊:随机的重定向

Mediawiki 避免在varnish中缓存来自特殊:随机的重定向,mediawiki,varnish,Mediawiki,Varnish,我正在使用varnish将MediaWiki安装中的内容提供给我的用户。按照vcl中的定义,几乎每个页面都被正确缓存: vcl 4.0; backend default { .host = "xxxx"; .port = "xxxx"; } backend thumbor { .host = "xxxx"; .port = "xxxx"; } acl purge { "xxxx"; } sub vcl_recv { set req.

我正在使用varnish将MediaWiki安装中的内容提供给我的用户。按照vcl中的定义,几乎每个页面都被正确缓存:

vcl 4.0;

backend default {
    .host = "xxxx";
    .port = "xxxx";
}

backend thumbor {
    .host = "xxxx";
    .port = "xxxx";
}

acl purge {
    "xxxx";
}

sub vcl_recv {
        set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
        set req.backend_hint= default;

        if (req.method == "PURGE") {
            if (!client.ip ~ purge) {
                return (synth(405, "Not allowed."));
            } else {
                return (purge);
            }
        }

        if (req.method != "GET" && req.method != "HEAD" &&
            req.method != "PUT" && req.method != "POST" &&
            req.method != "TRACE" && req.method != "OPTIONS" &&
            req.method != "DELETE") {
                return (pipe);
        }

        if (req.method != "GET" && req.method != "HEAD") {
            return (pass);
        }

    if (req.http.If-None-Match) {
        return (pass);
    }

    if (req.http.X-Debug-Server) {
        return (pass);
    }

    if (req.http.Cache-Control ~ "no-cache") {
        ban(req.url);
    }

        if (req.http.Accept-Encoding) {
          if (req.http.User-Agent ~ "MSIE 6") {
            unset req.http.Accept-Encoding;
          } elsif (req.http.Accept-Encoding ~ "gzip") {
            set req.http.Accept-Encoding = "gzip";
          } elsif (req.http.Accept-Encoding ~ "deflate") {
            set req.http.Accept-Encoding = "deflate";
          } else {
            unset req.http.Accept-Encoding;
          }
        }

    if (req.url ~ "(?i)\.(jpg|jpeg|jpe|png)$" && req.url ~ "(?i)/thumb/" && req.http.Accept ~ "(?i)image/webp" && req.http.x-no-thumbor != "yes") {
        set req.http.x-orig-url = req.url;
        set req.url = regsub(req.url,".*\/thumb\/(.*)","/unsafe/filters:format(webp)/thumb/\1");
        set req.backend_hint = thumbor;
    }

    if (req.url ~ "action=amp$") {
        unset req.http.Cookie;
        unset req.http.x-wap;
        return (hash);
    }

        if (req.http.Authorization || req.http.Cookie ~ "session" || req.http.Cookie ~ "Token") {
            return (pass);
        }

    if (req.http.Cookie ~ "droidwikiwikicookiewarning_dismissed=true") {
        set req.http.Cookie = "droidwikiwikicookiewarning_dismissed=true";
    } else {
        unset req.http.Cookie;
    }

    unset req.http.x-wap;
    if (req.http.User-Agent ~ "(?i)^(lg-|sie-|nec-|lge-|sgh-|pg-)|(mobi|240x240|240x320|320x320|alcatel|android|audiovox|bada|benq|blackberry|cdm-|compal-|docomo|ericsson|hiptop|htc[-_]|huawei|ipod|kddi-|kindle|meego|midp|mitsu|mmp\/|mot-|motor|ngm_|nintendo|opera.m|palm|panasonic|philips|phone|playstation|portalmmm|sagem-|samsung|sanyo|sec-|sendo|sharp|softbank|symbian|teleca|up.browser|webos)" && req.url !~ "(\?|&)(action=amp)") {
        set req.http.x-wap = "no";
    }

    if (req.http.Cookie ~ "mf_useformat=") {
        set req.http.x-wap = "no";
    }

        return (hash);
}

sub vcl_hash {
    hash_data(req.http.x-wap);
}

sub vcl_pipe {
        set req.http.connection = "close";
}

sub vcl_purge {
    if (req.url !~ "(\?|&)(action=amp)") {
        set req.http.X-Original = req.url;
        if (req.url ~ "&") {
            set req.url = req.url + "&action=amp";
        } else {
            set req.url = req.url + "?action=amp";
        }
        return (restart);
    }

    if (req.http.X-Original) {
        set req.url = req.http.X-Original;
    }

    if (!req.http.x-wap) {
        set req.http.x-wap = "no";
        return (restart);
    }
}

sub vcl_hit {
        if (req.method == "PURGE") {
            ban(req.url);
            return (synth(200, "Purged"));
        }

        if (!obj.ttl > 0s) {
            return (pass);
        }
}

sub vcl_miss {
        if (req.method == "PURGE")  {
            return (synth(200, "Not in cache"));
        }
}

sub vcl_deliver {
    if (resp.http.x-origin == "thumbor" && resp.status != 200) {
        set req.url = req.http.x-orig-url;
        set req.http.x-no-thumbor = "yes";
        return (restart);
    }

    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT";
    } else {
        set resp.http.X-Cache = "MISS";
    }
}

sub vcl_backend_error {
    set beresp.http.x-origin = beresp.backend.name;
}

sub vcl_backend_response {
        set beresp.grace = 120s;

        if (beresp.ttl < 48h) {
          set beresp.ttl = 48h;
        }       

        if (!beresp.ttl > 0s) {
          set beresp.uncacheable = true;
          return (deliver);
        }

        if (beresp.http.Set-Cookie) {
          set beresp.uncacheable = true;
          return (deliver);
        }

        if (beresp.http.Authorization && !beresp.http.Cache-Control ~ "public") {
          set beresp.uncacheable = true;
          return (deliver);
        }

        return (deliver);
}
vcl4.0;
后端默认值{
.host=“xxxx”;
.port=“xxxx”;
}
后端拇指{
.host=“xxxx”;
.port=“xxxx”;
}
acl清除{
“xxxx”;
}
子vcl_recv{
设置req.http.X-Forwarded-For=req.http.X-Forwarded-For+“,”+client.ip;
set req.backend\u hint=默认值;
如果(请求方法==“清除”){
如果(!client.ip~清除){
返回(synth(405,“不允许”);
}否则{
回流(吹扫);
}
}
如果(请求方法!=“GET”&&req方法!=“HEAD”&&
请求方法!=“PUT”&&req.method!=“POST”&&
请求方法!=“跟踪”&&req.method!=“选项”&&
请求方法!=“删除”){
回流管;
}
如果(请求方法!=“GET”&&req方法!=“HEAD”){
返回(通行证);
}
if(请求http.if无匹配){
返回(通行证);
}
if(请求http.X-Debug-Server){
返回(通行证);
}
if(req.http.Cache-Control~“无缓存”){
ban(请求url);
}
if(请求http接受编码){
if(req.http.User-Agent~“MSIE 6”){
unset req.http.Accept-Encoding;
}elsif(req.http.Accept-Encoding~“gzip”){
设置req.http.Accept-Encoding=“gzip”;
}elsif(req.http.Accept-Encoding~“deflate”){
设置req.http.Accept-Encoding=“deflate”;
}否则{
unset req.http.Accept-Encoding;
}
}
如果(req.url-“(?i)\(jpg | jpeg | jpe | png)$”&&req.url-“(?i)/thumb/”&&req.http.Accept-“(?i)image/webp”&&req.http.x-no-thumbor!=“是”){
设置req.http.x-orig-url=req.url;
set req.url=regsub(req.url,*\/thumb\/(.*),“/unsafe/filters:format(webp)/thumb/\1”);
设置req.backend\u hint=thumbor;
}
如果(req.url~“action=amp$”){
unset req.http.Cookie;
unset req.http.x-wap;
返回(散列);
}
if(req.http.Authorization | | req.http.Cookie~“session”| | req.http.Cookie~“Token”){
返回(通行证);
}
if(req.http.Cookie~“droidwikiwikicookiewarning\u disposed=true”){
set req.http.Cookie=“droidWikiCookieWarning\u disposed=true”;
}否则{
unset req.http.Cookie;
}
unset req.http.x-wap;
if(req.http.User-Agent)“(?i)^(lg-| sie-| nec-| lge-| sgh-| pg-|(mobi | 240x240 | 240x320 | 320x320 |阿尔卡特|安卓| audiovox bada |明基|黑莓| cdm-|康柏|爱立信| hiptop | htc[-]|(12)mmp\/| mmp \/|; mmp \;mmp \;mmp \;mmp \)mmp \;mmp \/124政政政政政政音;IPD;IPD;IPD;IPD;IPD;IPD;IPD;IPD;IPD;IPD;IPD;CDD;CDD;CDD;CDD;CDD;CDD;CDD;CDD;CDD;CDD;CDD;CDD;CDD;CDD;CDD;CDD;IPD;CDD;CDD;CDD;CDD;CDD;CDD;CDD;CDD;CDD;CDD;CDD;CDD;CDD;CDD;MED;MED;MED;MED;MEG;MEG;MEG;MEG;MEG;ME去去去去去去去去去去去去去去去去去去去去去去去去去去去去去去去去去去(动作=安培)“){
设置req.http.x-wap=“否”;
}
如果(req.http.Cookie~“mf_useformat=”){
设置req.http.x-wap=“否”;
}
返回(散列);
}
子vcl_散列{
散列数据(请求http.x-wap);
}
子vcl_管{
设置req.http.connection=“关闭”;
}
子vcl_吹扫{
如果(req.url!~“(\?|&)(action=amp)”){
设置req.http.X-Original=req.url;
如果(请求url~“&”){
设置req.url=req.url+“&action=amp”;
}否则{
设置req.url=req.url+“?操作=amp”;
}
返回(重启);
}
if(请求http.X-原件){
设置req.url=req.http.X-Original;
}
如果(!req.http.x-wap){
设置req.http.x-wap=“否”;
返回(重启);
}
}
子vcl_命中{
如果(请求方法==“清除”){
ban(请求url);
返回(synth(200,“净化”);
}
如果(!obj.ttl>0s){
返回(通行证);
}
}
子vcl_未命中{
如果(请求方法==“清除”){
返回(synth(200,“不在缓存中”);
}
}
副vcl_交付{
if(resp.http.x-origin==“thumbor”&&resp.status!=200){
设置req.url=req.http.x-orig-url;
设置req.http.x-no-thumbor=“是”;
返回(重启);
}
如果(对象命中率>0){
设置resp.http.X-Cache=“HIT”;
}否则{
设置resp.http.X-Cache=“MISS”;
}
}
子vcl_后端_错误{
设置beresp.http.x-origin=beresp.backend.name;
}
子vcl_后端_响应{
设置beresp.grace=120s;
如果(贝雷斯普ttl<48小时){
设置beresp.ttl=48小时;
}       
如果(!beresp.ttl>0s){
设置beresp.uncacheable=true;
归还(交付);
}
if(beresp.http.Set Cookie){
设置beresp.uncacheable=true;
归还(交付);
}
if(beresp.http.Authorization&&!beresp.http.Cache-Control~“public”){
设置beresp.uncacheable=true;
归还(交付);
}
归还(交付);
}
但是,当前配置也会导致缓存重定向。虽然这对于通常的重定向页面(如a文章重定向到B文章)来说是完全可以预期的行为,但对于
Special:Random
页面来说却出乎意料。每次我打开这个页面时,它都会指向一个随机页面。然而,由于重定向是由varnish缓存的,所以我总是被重定向到同一个页面

我已经想到了: 1.不缓存302重定向,但是,这可能会对其他重定向产生负面影响,我通常希望缓存这些重定向 2.在
Special:Random
上有一个url过滤器,但是,要做到这一点,我需要对wiki支持的所有语言进行例外处理,以确保它的可靠性。我对此不太高兴

我现在的问题是:有没有人知道如何使用一个不需要维护且只包含
特殊:随机
的规则来实现这一点?也许是一个解决方案,Wikime也在使用它