Tutorial: Setting up Varnish with Apache.


Varnish is an HTTP accelerator designed for content-heavy dynamic web sites. In contrast to other HTTP accelerators, such as Squid, which began life as a client-side cache, or Apache, which is primarily an origin server, Varnish was designed from the ground up as an HTTP accelerator.

Here’s how to set up varnish with Apache.
Tested on Ubuntu 10.04 (Lucid) Server LTS.


1. Install varnish:

2. Configure varnish to serve on 80 and fetch from 8008:

4. Set apache to listen on port 8008:


5. Set all vhosts to listen on port 8008:

Do the same for all other vhosts.

6. Restart Apache and Varnish:

7. Test varnish:

Notice the Varnish headers? There are also a couple of these. Age shows the age of the item in the Varnish cache.

Links

A good set of links can be found here:

Update: 17th Feb 2012

There is also a great set of instructions over at ocaoimh.ie.

  • vijay ahire

    is it possible to hide server ip from hacker using varnish ?

  • Peter Spencer

    Thanks for the how-to.

    Due to I am using Plesk I had to rebuild the conf:

    # mysql -uadmin -pcat /etc/psa/.psa.shadow -D psa -e’replace into misc (param, val) values (“http_port”, 8008)’
    # /usr/local/psa/admin/bin/httpdmng –reconfigure-all

    Nevertheless, I am constantly getting the following message:
    >>>
    It works!
    This is the default web page for this server.
    The web server software is running but no content has been added, yet.
    <<<

    Do you have any idea what I might miss?

    Thanks in advance!

  • alfanet

    Dear thank you for the tutorial I did all of this config but I always
    got error : varnish Error 503 Service Unavailable I changed timeout and
    the same , I just want to install varnish only , I dnt need apache or
    Ngnix

    please if you can help me and if you have working config file send me it

    thank you

  • It’s not possible to spoof you’re IP like that, users won’t be able to access your site if they get another IP than your server’s actual IP when doing a DNS lookup.

    • vijay ahire

      Hello Sebastian nnWeb server IP is : 6.6.6.6nnVarnish box is : 7.7.7.7nnThen youu2019d configure DNS for your.domain.com to resolve to 7.7.7.7nnYouu2019d configure Varnish on the Varnish box to listen on 7.7.7.7 and nhave 6.6.6.6 as a backend. You can either do this through the n/etc/varnish/default.vcl file, or by editing e.g. /etc/default/varnish n(on Debian) and specifying something in DAEMON_OPTSnnI got this from below post.. can you help me to configure this ? nnhttp://codepoets.co.uk/2012/fixing-remote_addr-when-behind-a-proxyvarnish-server/

  • This is because the config is for Varnish 2.x. I’ve converted this config to 3.x, if interested, you’ll find it here: http://pastebin.com/dGvxqWVZ

  • Howie

    I get this error using the vcl code above. Any ideas?
    Ubuntu 12.10

    Starting HTTP accelerator varnishd [fail]
    Message from VCC-compiler:
    Expected ‘;’ got ‘(‘
    (program line 174), at
    (‘input’ Line 27 Pos 22)
    purge(“req.url ~ ” req.url ” && req.http.host == ” req.http.host);
    ———————#————————————————————

    Running VCC-compiler failed, exit 1

    VCL compilation failed

  • Mike

    Thanks, you’re my hero! The documentation at the Varnish site is really bad, and steps 2 and 5 are missing. I figured out step 5 on my own, but the /etc/default/varnish settings file is not mentioned at all in the Varnish 3.x documentation and without it Apache and Varnish never connect.

    Now everything’s up and running, and even though I haven’t yet adjusted the VCL to the particulars of my site things are already much snappier.

    You have my heartfelt thanks!

    • Hi Mike, thanks for the reply. I’m glad the article helped.

  • Hello

    I am planning to configure Varnish on my server to hide me server ip
    consider my server ip is 66.66.66.66 and I want to configure varnish in such a way, when anyone tries to locate my server ip using any dns checker tool, it should show 99.99.99.99
    to users.

    can you tell me how can i do this. I tried alot and alot but no result.

    help needed.

    Thanks
    Yatin

  • Hello

    I am planning to configure Varnish on my server to hide me server ip
    consider my server ip is 66.66.66.66 and I want to configure varnish in such a way, when anyone tries to locate my server ip using any dns checker tool, it should show 127.0.0.1
    to users.

    can you tell me how can i do this. I tried alot and alot but no result.

    help needed.

    Thanks
    Yatin

  • Hi,

    We want to varnish with apache on centos 64 OS. Also we think about install ngnix on that server. Is it all fine with these configurations?

    Thank you

    • Hi Oyan

      We use Varnish with NGinx on some of our servers and the performance is phenominal. This set-up should work OK on Centos but you may need to change a couple of things.

  • Hi,

    Is varnish making proxy to apache? I want to use varnish with apache + cpanel + whm. Is it make any problem, i’m not sure about that.

    Thanks,
    Ugur

  • stef

    i spent hours on configuring 3 files and it didnt work, but it seems i finally solved my problem in step5 which missess in most tutorials about “how to configure varnish”

  • Will this work on Window’s with Apache2? If so what would be the changes.

    Thanks.

    great work…

    douglas – @ Hawaii the Big Island

    • Hi

      I don’t use Windows so unfortunately I can’t help you.

      Andrew