Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to use Varnish to show the non-logged in users a cached version of my website. I'm able to perfectly detect if the user is logged in or out, but I can't cache pages without extensions.

There is no file extension because nginx is rewriting the URL to a php script(so caching .php does not work).

For example I'd like varnish to cache: example.com example.com/forum/ example.com/contact/

I have tried

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

but nothing seems to work... any help?

EDIT:

It still gives me X-Cache: MISS whenever I go to a extensionless page...

Website is at http://www.atherys.com

And the current VCL is...

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

Also, 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
share|improve this question
1  
Where are you putting that configuration? Are you certain that it's not being cached - can we see the output for a relevant request from varnishlog? Varnish doesn't pay any attention to file extensions (or lack thereof) by default. –  Shane Madden Dec 5 '12 at 4:36
add comment

2 Answers

Though I did have couple queries before answering this question, but I don't have rights to comment, so putting here.

First thing is that from all the above three rules, you need only one rule, this one:

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

The above rule should be put in vcl_recv section.

Once you put that rule there, you need another entry in the vcl_fetch section, something like this:

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

This will define the ttl for the URL and will enable caching for the URL for next 7 days.

If it doesn't work, would be great if you can post your default.vcl file here and at the same time, if your site is public, then a link to that, so that We could take a look further and figure out what's happening.

share|improve this answer
    
Hey! Thanks for the suggestion, but it doesn't work. I've added more data to the question.... –  Sigurs Dec 5 '12 at 18:25
    
So, there are couple of problems I see with this vcl file. 1. In this line: if (req.http.Cookie ~ "xf_cache=1" && req.request == "GET") { I believe that it should be just cookie name. Should be like this: if (req.http.Cookie ~ "xf_cache" && req.request == "GET") { 2. The other one which I believe you didn't notice (unless you are doing it intentionally) is that you are not caching images. As you are rewriting the URL's to not have jpg in the end. That's what you rule says. Removing $ mark from the end should fix it. Please try to fix the cookie thing and hopefully it should work –  GeekRide Dec 5 '12 at 19:26
    
Actually, the cookie thing is intentional, xf_cache with value 1 is applied to non-registered users when value 0 is for registered users. I'm basicly trying to cache everything I can when the xf_cache=1. I removed the $ marks and now images are working perfectly, but still 'folders' aren't caching, like /forum/. –  Sigurs Dec 5 '12 at 19:45
add comment

That's quite a complex setup and I have the impression that it has grown to a point that it became very unwieldy and hard to understand.

Points that I see:

  • Making varnish lookup in vcl_recv doesn't seem to be the problem
  • The sample request you show indicates a Vary header. By default (as you didn't specifiy a vcl_fetch) this should cause varnish to not insert the object into the cache.

I recommend trimming down the config to a smaller version first and see whether you can get it to cache the some sample pages you are talking about. Try avoiding the Vary header and Cookies if you can.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.