Varnish

From wiki.techunit.org
Jump to: navigation, search

Introduction

Varnish is an HTTP accelerator designed for content-heavy dynamic web sites as well as heavily consumed APIs. In contrast to other web accelerators, such as Squid, which began life as a client-side cache, or Apache and nginx, which are primarily origin servers, Varnish was designed as an HTTP accelerator. Varnish is focused exclusively on HTTP, unlike other proxy servers that often support FTP, SMTP and other network protocols.

  • Realized on debian 8
  • Varnish version : 4.0.2-1

See also

Configure Varnish with Apache to provide HTTPS

Configuration

Installation

aptitude install varnish -y

Minimal

This configuration will cache HTML page content powered by Apache Web Server on HTTP protocol.

Required

Realization

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

# New configuration
DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"
  • Configure backend, Edit /etc/varnish/default.vcl
#Set the port you configured in apache
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}
  • Restart Varnish
service varnish restart

Multiple backend example

  • Edit /etc/varnish/default.vcl
#Backends configuration
backend default {
    .host = "127.0.0.1";
    .port = "80";
}

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

backend other_backend {
    .host = "10.0.0.2"; #or hostname
    .port = "80";
}

#ACL
sub vcl_recv {
    if (req.http.host == "mail.example.com") {
        set req.backend = local_backend1;
    } 
    else if (req.http.host == "www.example2.com") {
        set req.backend = other_backend;
    }
    else {
        set req.backend = default;
    }
}

Sources

Elliot
techUnit's cofounder