Skip to main content

How to properly set up Varnish for Symfony2 sites?


I have a website (with ESI) that uses Symfony2 reverse proxy for caching. Average response is around 100ms. I tried to install Varnish on server to try it out. I followed guide from Symfony cookbook step by step, deleted everything in cache folder, but http_cache folder was still created when I tried it out. So I figured I could try to comment out $kernel = new AppCache($kernel); from app.php . That worked pretty well. http_cache wasn't created anymore and by varnishstat, Varnish seemed to be working:




12951 0.00 0.08 cache_hitpass - Cache hits for pass
1153 0.00 0.01 cache_miss - Cache misses



That was out of around 14000 requests, so I thought everything would be alright. But after echoping I found out responses raised to ~2 seconds.



Apache runs on port 9000 and Varnish on 8080. So I echoping using echoping -n 10 -h http://servername/ X.X.X.X:8080 .



I have no idea what could be wrong. Are there any additional settings needed to use Varnish with Symfony2? Or am I simply doing something wrong?





Per requests, here's my default.vcl with modifications I've done so far.



I found 2 issues with Varnish's default config:



  • it doesn't cache requests with cookies (and everyone in my app has session assigned)

  • it ignores Cache-Control: no-cache header



So I added conditions for these cases to my config and it performs fairly well now (~175 req/s up from ~160 with S2 reverse proxy - but honestly, I expected bit more). I just have no idea how to check if it's all set ok, so any inputs are welcome.



Most of pages have cache varied by cookie, with s-maxage 1200. Common ESI includes aren't varied by cookie, with s-maxage quite low (articles, article lists). User profile pages aren't cached at all ( no-cache ) and I'm not really sure if ESI includes on these are even being cached by Varnish. Only ESI that's varied by cookies is header with user specific information (that's on 100% of pages).



Everything in this post is Varnish 3.X specific (I'm personally using 3.0.2).



Also, after few weeks of digging into this, I have really no idea what I'm doing anymore, so if you find something odd in configs, just let me know.



enter image description here


Source: Tips4allCCNA FINAL EXAM

Comments

  1. If that's your entire configuration, the vcl_recv is configured twice.

    In the pages you want to cache, can you send the caching headers? This would make the most sense, since images probably already have your apache caching headers and the app logic decide the pages that can be actually cached, but you can force this in varnish also.

    You could use a vcl_recv like this:

    # Called after a document has been successfully retrieved from the backend.
    sub vcl_fetch {

    # set minimum timeouts to auto-discard stored objects
    # set beresp.prefetch = -30s;
    set beresp.grace = 120s;

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

    if (!beresp.cacheable)
    {pass;}

    if (beresp.http.Set-Cookie)
    {pass;}

    # if (beresp.http.Cache-Control ~ "(private|no-cache|no-store)")
    # {pass;}

    if (req.http.Authorization && !beresp.http.Cache-Control ~ "public")
    {pass;}

    }


    This one caches, in varnish, only the requests that are set cacheable. Also, be aware that your configuration doesn't cache requests with cookies.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex