缓存时不使用文件扩展名

缓存时不使用文件扩展名

我正在尝试使用 Varnish 向未登录的用户显示我网站的缓存版本。我可以完美地检测用户是否已登录或已退出,但如果没有扩展程序,我无法缓存页面。

没有文件扩展名,因为 nginx 正在将 URL 重写为 php 脚本(因此缓存 .php 不起作用)。

例如我希望 varnish 缓存:example.com example.com/forum/ example.com/contact/

我努力了

if (req.request == "GET" && req.url ~ "^/") {
return(lookup);
}
if (req.request == "GET" && req.url ~ "") {
return(lookup);
}
if (req.request == "GET" && req.url ~ "/") {
return(lookup);
}

但似乎没有什么作用...有什么帮助吗?

编辑:

每当我进入没有扩展的页面时,它仍然会给我 X-Cache:MISS......

网站位于http://www.atherys.com

当前的 VCL 是...

# This is a basic VCL configuration file for varnish.  See the vcl(7)
# man page for details on VCL syntax and semantics.
#
# Default backend definition.  Set this to point to your content
# server.
#
backend default {
.host = "127.0.0.1";
.port = "8080";
}


/* Only permit cluster to purge files from cache */
acl purge {
"localhost";
}


sub vcl_recv {
        /* Before anything else we need to fix gzip compression */
    if (req.http.Accept-Encoding) {
            if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
                    # No point in compressing these
                    remove req.http.Accept-Encoding;
            } else if (req.http.Accept-Encoding ~ "gzip") {
                    set req.http.Accept-Encoding = "gzip";
            } else if (req.http.Accept-Encoding ~ "deflate") {
                    set req.http.Accept-Encoding = "deflate";
            } else {
                    # unknown algorithm
                    remove req.http.Accept-Encoding;
            }
    }

    if (req.request == "PURGE") {
            if (!client.ip ~ purge) {
                    error 405 "Not allowed.";
            }
            /* Always purge by URL rather than going via vcl_hash
            as it hashes other factors which break purging */
            purge_url(req.url);
            error 200 "Purged";
    }



    #Remove cookies,remove queries and cache everything from content.gazamo-cdn.com
    if (req.http.host == "content.gazamo-cdn.com") {
            remove req.http.Cookie;
            set req.url = regsub(req.url, "\?.*$", "");
    }

    #Atherys specific, if not logged in = cache; if logged in = pass
    if (req.http.host == "atherys.com" || req.http.host == "www.atherys.com") {
            if (req.http.Cookie ~ "xf_cache=1" && req.request == "GET") {
                    return(lookup);
                    if (req.request == "GET" && req.url ~ "^/") {
                            return(lookup);
                    }

            }
    }

    # always cache these items:

    if (req.request == "GET" && req.url ~ "\.(js|css)") {
            remove req.http.Cookie;
            return(lookup);
    }

    ## images
    if (req.request == "GET" && req.url ~ "\.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|img|tga|wmf)$") {
            remove req.http.Cookie;
            return(lookup);
    }

    ## various other content pages
    if (req.request == "GET" && req.url ~ "\.(css|html)$") {
            remove req.http.Cookie;
            return(lookup);
    }

    ## multimedia
    if (req.request == "GET" && req.url ~ "\.(svg|swf|ico|mp3|mp4|m4a|ogg|mov|avi|wmv)$") {
            remove req.http.Cookie;
            return(lookup);
    }

    ## various other content pages
    if (req.request == "GET" && req.url ~ "\.(css|html)$") {
            remove req.http.Cookie;
            return(lookup);
    }

    ## multimedia
    if (req.request == "GET" && req.url ~ "\.(svg|swf|ico|mp3|mp4|m4a|ogg|mov|avi|wmv)$") {
            remove req.http.Cookie;
            return(lookup);
    }

    ## xml

    if (req.request == "GET" && req.url ~ "\.(xml)$") {
            remove req.http.Cookie;
            return(lookup);
    }

    #Do not cache
    if (req.request != "GET" && req.request != "HEAD") {
            return(pipe);
    }
    if (req.http.Authenticate || req.http.Authorization) {
            return(pass);
    }

    ### don't cache authenticated sessions
    #if (req.http.Cookie && req.http.Cookie ~ "authtoken=") {
    #        return(pipe);
    #}


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

另外,varnishlog:

0 CLI          - Rd ping
0 CLI          - Wr 200 PONG 1354732729 1.0
0 CLI          - Rd ping
0 CLI          - Wr 200 PONG 1354732732 1.0
12 SessionOpen  c 86.50.90.54 62847 :80
12 ReqStart     c 86.50.90.54 62847 1225125294
12 RxRequest    c GET
12 RxURL        c /forum/
12 RxProtocol   c HTTP/1.1
12 RxHeader     c Host: www.atherys.com
12 RxHeader     c User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0
12 RxHeader     c Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
12 RxHeader     c Accept-Language: fi-fi,fi;q=0.8,en-us;q=0.5,en;q=0.3
12 RxHeader     c Accept-Encoding: gzip, deflate
12 RxHeader     c Connection: keep-alive
12 RxHeader     c Referer: http://www.atherys.com/forum/
12 RxHeader     c Cookie: xf_session=xxxxxxxxxxxxxxxx; xf_cache=1
12 RxHeader     c If-Modified-Since: Wed, 05 Dec 2012 18:27:16 GMT
12 VCL_call     c recv
12 VCL_return   c lookup
12 VCL_call     c hash
12 VCL_return   c hash
12 VCL_call     c miss
12 VCL_return   c fetch
14 BackendClose - default
14 BackendOpen  b default 127.0.0.1 59671 127.0.0.1 8080
12 Backend      c 14 default default
14 TxRequest    b GET
14 TxURL        b /forum/
14 TxProtocol   b HTTP/1.1
14 TxHeader     b Host: www.atherys.com
14 TxHeader     b User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0
14 TxHeader     b Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
14 TxHeader     b Accept-Language: fi-fi,fi;q=0.8,en-us;q=0.5,en;q=0.3
14 TxHeader     b Referer: http://www.atherys.com/forum/
14 TxHeader     b Cookie: xf_session=xxxxxxxxx; xf_cache=1
14 TxHeader     b Accept-Encoding: gzip
14 TxHeader     b X-Varnish: 1225125294
14 RxProtocol   b HTTP/1.1
14 RxStatus     b 200
14 RxResponse   b OK
14 RxHeader     b Server: nginx
14 RxHeader     b Date: Wed, 05 Dec 2012 18:38:54 GMT
14 RxHeader     b Content-Type: text/html; charset=UTF-8
14 RxHeader     b Content-Length: 16013
14 RxHeader     b Connection: keep-alive
14 RxHeader     b Expires: Thu, 19 Nov 1981 08:52:00 GMT
14 RxHeader     b Cache-control: private, max-age=0
14 RxHeader     b Last-Modified: Wed, 05 Dec 2012 18:38:54 GMT
14 RxHeader     b Content-Encoding: gzip
14 RxHeader     b Vary: Accept-Encoding
12 TTL          c 1225125294 RFC 0 1354732734 0 0 0 0
12 VCL_call     c fetch
12 VCL_return   c deliver
12 ObjProtocol  c HTTP/1.1
12 ObjStatus    c 200
12 ObjResponse  c OK
12 ObjHeader    c Server: nginx
12 ObjHeader    c Date: Wed, 05 Dec 2012 18:38:54 GMT
12 ObjHeader    c Content-Type: text/html; charset=UTF-8
12 ObjHeader    c Expires: Thu, 19 Nov 1981 08:52:00 GMT
12 ObjHeader    c Cache-control: private, max-age=0
12 ObjHeader    c Last-Modified: Wed, 05 Dec 2012 18:38:54 GMT
12 ObjHeader    c Content-Encoding: gzip
12 ObjHeader    c Vary: Accept-Encoding
14 Length       b 16013
14 BackendReuse b default
0 CLI          - Rd ping
0 CLI          - Wr 200 PONG 1354732735 1.0
12 VCL_call     c deliver
12 VCL_return   c deliver
12 TxProtocol   c HTTP/1.1
12 TxStatus     c 200
12 TxResponse   c OK
12 TxHeader     c Server: nginx
12 TxHeader     c Content-Type: text/html; charset=UTF-8
12 TxHeader     c Expires: Thu, 19 Nov 1981 08:52:00 GMT
12 TxHeader     c Cache-control: private, max-age=0
12 TxHeader     c Last-Modified: Wed, 05 Dec 2012 18:38:54 GMT
12 TxHeader     c Content-Encoding: gzip
12 TxHeader     c Vary: Accept-Encoding
12 TxHeader     c Content-Length: 16013
12 TxHeader     c Date: Wed, 05 Dec 2012 18:38:54 GMT
12 TxHeader     c X-Varnish: 1225125294
12 TxHeader     c Age: 0
12 TxHeader     c Via: 1.1 varnish
12 TxHeader     c Connection: keep-alive
12 TxHeader     c X-Cache: MISS
12 Length       c 16013
12 ReqEnd       c 1225125294 1354732734.723051310 1354732735.044378281 0.000048399 0.204994202 0.116332769 
12 Debug        c "herding"
12 ReqStart     c 86.50.90.54 62847 1225125295
12 RxRequest    c GET
12 RxURL        c /cron.php?1354732734
12 RxProtocol   c HTTP/1.1
12 RxHeader     c Host: www.atherys.com
12 RxHeader     c User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0
12 RxHeader     c Accept: image/png,image/*;q=0.8,*/*;q=0.5
12 RxHeader     c Accept-Language: fi-fi,fi;q=0.8,en-us;q=0.5,en;q=0.3
12 RxHeader     c Accept-Encoding: gzip, deflate
12 RxHeader     c Connection: keep-alive
12 RxHeader     c Referer: http://www.atherys.com/forum/
12 RxHeader     c Cookie: xf_session=xxxxxx; xf_cache=1
12 VCL_call     c recv
12 VCL_return   c lookup
12 VCL_call     c hash
12 VCL_return   c hash
12 VCL_call     c miss
12 VCL_return   c fetch
12 Backend      c 14 default default

答案1

虽然在回答这个问题之前我确实有几个疑问,但我没有权利评论,所以就放在这里。

首先,从以上三条规则中,您只需要一条规则,即这条规则:

if (req.request == "GET" && req.url ~ "^/") {
   return(lookup);
}

上述规则应该放在vcl_recv部分。

一旦你把这个规则放在那里,你就需要 vcl_fetch 部分中的另一个条目,如下所示:

if (req.url ~ "^/") {
   set beresp.ttl = 7d;
}

这将定义 URL 的 ttl 并启用接下来 7 天的 URL 缓存。

如果它不起作用,如果您可以在这里发布您的 default.vcl 文件就太好了,同时,如果您的网站是公开的,那么就发布到该网站的链接,这样我们就可以进一步查看并弄清楚发生了什么。

答案2

这是一个相当复杂的设置,我的印象是它已经发展到非常笨重和难以理解的地步。

我看到的要点:

  • 在 vcl_recv 中进行 varnish 查找似乎不是问题
  • 您显示的示例请求指示 Vary 标头。默认情况下(因为您没有指定 vcl_fetch),这应该会导致 varnish 不将对象插入缓存中。

我建议先将配置缩减为较小的版本,然后看看是否可以让它缓存您正在讨论的一些示例页面。如果可以,请尝试避免使用 Vary 标头和 Cookies。

相关内容