Pythontr

husonet | Tarih: 12.09.2011

varnish cache kurulumu ve konfigürasyonu

varnish cache kurulumu (Load Balancing)

Varnish Cache facebook un kullandığı Cache yöntemidir bende geçenlerde okudum kurulumunu yaptım şu an testlerini yapıyorum incelemek isteyenler için faydalı olucağını düşündüm.


# güncellemeler için
aptitude update && aptitude full-upgrade

# varnish yüklemesi için apt paket düzenlemesi
curl http://repo.varnish-cache.org/debian/GPG-key.txt | apt-key add -
vim /etc/apt/sources.list.d/varnish.list
deb http://repo.varnish-cache.org/debian/ squeeze varnish-3.0

# varnis-cache i yükleyelim
aptitude install varnish

vim /etc/default/varnish
#şimdilik loglama yapılacak klasör için belirleyelim
INSTANCE=default

#-a parametresi varnishi dinleyecek kısım
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s file,/var/lib/varnish/$INSTANCE/varnish_storage.bin,1GB"

vim /etc/varnish/default.vcl
backend default {
.host = "127.0.0.1";
.port = "8080";
.connect_timeout = 600s;
.first_byte_timeout = 600s;
.between_bytes_timeout = 600s;
}

# apache web servisimizin dinlediği portları düzeltelim
vim /etc/apache2/ports.conf
NameVirtualHost *:8080
Listen 8080

# apache host ayar dosyamızıda düzeltelim
vim /etc/apache2/sites-available/default
<VirtualHost *:8080>

mkdir /var/lib/varnish/default
chown varnish: /var/lib/varnish/default


#kontrol edelim
netstat -anp --tcp --udp | grep LISTEN

# cache dosyamız ne alemde cachliyormu acaba ?
du -k /var/lib/varnish/default/varnish_storage.bin

# bu kısım vcl düzeltme örneğidir.
# analytics ayarlarımızı yapalım
sub vcl_recv {
# dışardan gelen ipleri olduğu gibi apachemize aktaralım
remove req.http.X-Forwarded-For;
set req.http.X-Forwarded-For = client.ip;

# Remove has_js and Google Analytics __* cookies.
set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|has_js)=[^;]*", "");
# Remove a ";" prefix, if present.
set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
# Remove empty cookies.
if (req.http.Cookie ~ "^\s*$") {
unset req.http.Cookie;
}

# image dosyalarını cachle
if ((req.url ~ "\.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|img|tga|wmf)$"))
{
unset req.http.cookie;
return(lookup);
}

# detay sayfalar cacheden gelsin
if (req.url ~ "\/detay\/")
{
unset req.http.cookie;
return(lookup);
}

# xml
if (req.url ~ "\.(xml)$") {
unset req.http.cookie;
return(lookup);
}

# phpmyadmin
if (req.url ~ "\/mytest\/|sql\.php") {
return(pass);
}
unset req.http.Cookie;
return (lookup);
}

# Redirection routine
sub vcl_error {
if (obj.status == 750) {
set obj.http.Location = obj.response;
set obj.status = 302;
return(deliver);
}
}

sub vcl_fetch {

#--- Php başlangıç
# phpmyadmin
if (req.url ~ "\/mytest\/|sql\.php") {
return (hit_for_pass);
}
#--- Php Son

# xml
if (req.url ~ "\.(xml)$") {
return (hit_for_pass);
}

unset beresp.http.Set-Cookie;
set beresp.ttl = 60s;

if (req.url ~ "\.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|img|tga|wmf)$")
{
unset beresp.http.set-cookie;
set beresp.ttl = 48h;
}

# detay sayfalar 10 saniye ne varsa cacheden gelsin
if (req.url ~ "\/detay\/")
{
unset beresp.http.set-cookie;
set beresp.ttl = 60s;
}

return(deliver);
}

sub vcl_pipe {
# Note that only the first request to the backend will have
# X-Forwarded-For set. If you use X-Forwarded-For and want to
# have it set for all requests, make sure to have:
# set bereq.http.connection = "close";
# here. It is not set by default as it might break some broken web
# applications, like IIS with NTLM authentication.
return (pipe);
}

sub vcl_pass {
return (pass);
}

sub vcl_hit {
return (deliver);
}

Loglamalarda ipler gözükmesi için şu modulu kurmamız lazım


aptitude install libapache2-mod-rpaf

Apache configürasyon dosyamızı düzenleyelim dışardan bağlantı kuran makinaların iplerini loglamak için.


vim /etc/apache2/apache2.conf
RPAFenable On
RPAFproxy_ips 127.0.0.1
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" varnishcombined