Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Setting up varnish with Apache

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 546
    Comment on it

    Before we begin with the setup of the varnish with Apache, let us know a bit about Varnish and then move ahead with configuration of varnish with apache with some examples that gives you an idea of basic settings for the server.

    Varnish is an HTTP Accelerator that helps delivering content heavy dynamic website faster to the clients by storing the content on the memory of the server. So the performance of the sites even after using varnish is some what dependent on the hardware resources available with server machine.

    We will begin with the installation of the varnish on the Linux Box along with apache and then configuring the same to deliver the content.

    Installation (Varnish):

    If you are on RHEL 5 or a compatible distribution, use:

    1. rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-3.0.el5.rpm 
    2. yum install varnish 
    

    For RHEL 6 and compatible distributions, use:

    1. rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-3.0.el6.rpm 
    2. yum install varnish 
    

    For Debian based distributions, use:

    To use the varnish-cache.org repository and install Varnish under Debian 7 ("wheezy") do the following as root:

    1. apt-get install apt-transport-https 
    2. curl https://repo.varnish-cache.org/GPG-key.txt | apt-key add - 
    3. echo "deb https://repo.varnish-cache.org/debian/ wheezy varnish-4.0" >> /etc/apt/sources.list.d/varnish-cache.list 
    4. apt-get update 
    5. apt-get install varnish 
    

    For Debian 8 ("jessie") replace wheezy above with jessie.

    For ubuntu distribution, use:

    To use the varnish-cache.org repository and install Varnish under Ubuntu 12.04 (Precise Pangolin) do the following as root:

    1. apt-get install apt-transport-https 
    2. curl https://repo.varnish-cache.org/GPG-key.txt | apt-key add - 
    3. echo "deb https://repo.varnish-cache.org/ubuntu/ precise varnish-4.0" >> /etc/apt/sources.list.d/varnish-cache.list 
    4. apt-get update 
    5. apt-get install varnish 
    

    Installation (Apache)

    For RHEL based distributions, use:

    yum install httpd
    

    For Debian based distributions, use:

    apt-get install apache
    

    After we have the packages installed lets now configure the daemon options for the varnish server and understand the directives.

    The default configuration file for the varnish daemon is located in /etc/default/varnish which can be edited using command

    sudo nano /etc/default/varnish
    

    Uncomment all of the lines under DAEMON_OPTS under Alternative 2 and edit the code to look like below

     DAEMON_OPTS="-a :80 \
                 -T localhost:6082 \
                 -f /etc/varnish/default.vcl \
                 -S /etc/varnish/secret \
                 -s malloc,256m"
    

    We would explain the 3 important directives now.

    -a :80 tells the port on which the daemon would run.

    -f /etc/varnish/default.vcl tells the configuration file for the server.

    -s malloc,256m tells that the cache would be stored in the memory and 256 MB is the limit set for the same.

    Now we edit the varnish configuration by using command

    sudo nano /etc/varnish/default.vcl
    

    For our example a basic on the configuration should look something like this.

    backend default {
        .host = "127.0.0.1";
        .port = "8080";
    }
    

    Now we would configure apache to act as a server behined varnish. On RHEL based distribution, use:

    sudo nano /etc/httpd/conf/httpd.conf
    

    Find and edit Listen directive from

    Listen *:80 to
    Listen *:8080
    

    On Debian based distribution, use:

    sudo nano /etc/apache2/conf/sites-enabled/000-default.conf
    

    and add lines

    NameVirtualHost 127.0.0.1:8080
    Listen 127.0.0.1:8080
    

    Now change the port on the virtual host directive to

     <VirtualHost 127.0.0.1:8080>
    

    then restart the apache server using commands

    sudo service apache2 restart on a debian based distribution.
    sudo service httpd restart on a RHEL based distribution.
    

    and

    sudo service varnishd restart to restart varnish server.
    

    To check the stats of varnish server at any time use command

    varnishstat
    

    To confirm if the page is being served from varnish or not you can check the response headers for headers Via: which would have a value like 1.1 varnish and presence of another http header X-Varnish. These confirm that the page being served is done through varnish or not.

    Now to a basic configuration that may be required by most of the sites that is to skip some url's so that they are never served by varnish and only served by Apache. For example if you have a site that has an admin interface you may not want the pages for admin to come from cache and want these to execute on server every time these are requested this can be done easily by changing the configuration on varnish.

    In the VCL file overide a function vcl_recv which is called everytime varnish receives a request.

    sub vcl_recv
    {
    if(req.url ~ ^/admin)
    {
        return (pass);
    }
    }
    

    In the above case we check if the URI requests begin with /admin then Varnish would not cache the pages.

    These are some of the basic settings for varnish to work in front of apache to speed up your sites.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: