Outils personnels

Varnish cache

De wikiGite

Installation

Recuperer le paquet du depot varnish sur http://repo.varnish-cache.org/redhat/varnish-3.0/ Pour Centos 6 :

yum install http://repo.varnish-cache.org/redhat/varnish-3.0/el6/noarch/varnish-release/varnish-release-3.0-1.el6.noarch.rpm

Pour Centos 5:

wget http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release/varnish-release-3.0-1.noarch.rpm
rpm -ivh varnish-release-3.0-1.noarch.rpm

Installer Varnish

# yum install varnish

Editer /etc/sysconfig/varnish :

VARNISH_LISTEN_PORT=6081

Éditer la configuration de varnish pour la rediretion vers vers apache /etc/varnish/default.vcl :

backend default {
   .host = "10.0.0.1";
   .port = "80";
   #pour eviter les erreurs 503
   .connect_timeout = 600s;
   .first_byte_timeout = 600s;
   .between_bytes_timeout = 600s;
}

Lancer varnish

service varnish start

Lancer varnish au démarrage

# chkconfig varnish on 

Read more: http:www.how2centos.com/install-varnish-centos-6/#ixzz2I82TuuYJ

Pour ne pas changer le port d'Apache on redirige le port 80 en entrée avec iptables vers varnish (tous ce qui vient de l’extérieur est natté du port 80 vers le port 6081). Varnish renverra ensuite vers le port 80 d'Apache :

# iptables -t nat  -A PREROUTING -i eth0 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 6081
# iptables-save

iptables-save conserve cette règle aux redémarrages de CentOS. Pour une autre distribution, voir comment relancer cette règle au boot, ou l'ajouter dans rc.local, par exemple.

Rajouter ces raccourci dans ~/.bashrc de root :

alias varnishon='iptables -t nat  -A PREROUTING -i eth0 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 6081'
alias varnishoff='iptables -t nat  -D PREROUTING -i eth0 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 6081'
alias varnishstatus='iptables -L -t nat |grep -q 6081; if [ "test$?" = "test0" ]; then echo -e "Varnish On"; else echo -e "Varnish Off"; fi ; iptables -L -t nat'

Configuration

Varnish en mémoire plutôt que sur disque

Changer :

MEMLOCK=82000
...
VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"

par :

MEMLOCK=512000
...
VARNISH_STORAGE="malloc,512M"

pour augmenter la mémoire autorisée à 512M et forcer Varnish en mémoire sur ces 512M.

Pour exclure un hôte

vim /etc/varnish/default.vcl

sub vcl_recv {
    # Don't cache domain1.com or domain2.org - optional www
    if (req.http.host ~ "(www\.)?(domain1.com|domain2.org)\.(com|nl|org|dev|local)") {
        return (pass);
    }
}

sub vcl_deliver {
    # You can optionally set a reponse header so it's easier for you to debug if Varnish really didn't cache objects for the host
    # Don't cache domain1.com or domain2.org - optional www
    if (req.http.host ~ "(www\.)?(domain1.com|domain2.org)\.(com|nl|org|dev|local)") {
        set resp.http.X-Cache = "EXCLUDED";
    }
}

Relancer varnish :

service varnish restart

Sources