Varnish: Difference between revisions

From ICO wiki
Jump to navigationJump to search
Rsepp (talk | contribs)
Rsepp (talk | contribs)
Line 163: Line 163:
RPAFproxy_ips 127.0.0.1 xxx.xxx.xxx.xxx
RPAFproxy_ips 127.0.0.1 xxx.xxx.xxx.xxx
</IfModule></pre>
</IfModule></pre>
Lisa Varnishi serveri IP 127.0.0.1 asemele
Lisa Varnishi serveri IP xxx.xxx.xxx.xxx asemele


Apachele restart
Apachele restart

Revision as of 11:27, 7 May 2012

Koostajad

Rene Sepp, Kersti Lang, Carolys Kallas

Kevad 2012

Sissejuhatus

Varnishit kasutatakse veebilehekülgede kiirendamiseks. See installeeritakse HTTP serveri ette ning tehakse vastavad muutused sisu cachimiseks. Varnish cache tavaliselt kiirendab andmete laadimiset 300-1000 kordselt sõltuvalt veebilehe ülesehitusest. Varnish cache hoiab veebilehti vahemälus kiirendades sellega javaskriptide ning piltide laadimist. Lisaks kasutatakse seda ka koormuse tasakaalustamiseks ning turvalisuse suurendamiseks.

Eeldused

Antud lahendus on testitud Ubuntu Server 10.10 peal, kuid võib töötada ka teiste Ubuntu versioonidega ning muude Linuxi distributsioonidega.

Eelduseks on töötav veebileht, näiteks wordpressi leht. Kui seda tehtud pole, siis seda kirjeldab WordPress turvamise labor. https://wiki.itcollege.ee/index.php/WordPress_turvamine

Varnishi paigaldamine turvalisuse suurendamiseks

Varnish peatab mittetäielikel http päringutel jõudmast Apache veebiserverini. Põhineb "Putting Varnish In Front Of Apache On Ubuntu/Debian" artiklil, mis asub siin http://www.howtoforge.com/putting-avarnish-in-front-of-apache-on-ubuntu-debian

Kontrollime kas on installitud kõige uuem varnishi versioon.

sudo apt-get install curl
sudo curl http://repo.varnish-cache.org/debian/GPG-key.txt | apt-key add -
sudo echo "deb http://repo.varnish-cache.org/debian/ $(lsb_release -s -c) varnish-2.1" >> /etc/apt/sources.list 

Apache konfiguratsiooni muutmine

Muudame varnishi konfiguratsiooni faili, et varnish kuulaks defaultina port 80.

nano /etc/default/varnish

Muuda "DAEMON_OPTS="-a *:6081 \" selliseks

DAEMON_OPTS="-a *:80 \

ctrl-O ja muudame nime millekski muuks, nt mysite.vcl

Kopeerime /etc/varnish/default.vcl

cp /etc/varnish/default.vcl /etc/varnish/mysite.vcl

Täiendame /etc/varnish/mysite.vcl faili.

## Redirect requests to Apache, running on port 8000 on localhost
backend apache {
        .host = "127.0.0.1";
        .port = "8000";
}
## Fetch
sub vcl_fetch {
		## Remove the X-Forwarded-For header if it exists.
        remove req.http.X-Forwarded-For;
		
		## insert the client IP address as X-Forwarded-For. This is the normal IP address of the user.
        set    req.http.X-Forwarded-For = req.http.rlnclientipaddr;
		## Added security, the "w00tw00t" attacks are pretty annoying so lets block it before it reaches our webserver
        if (req.url ~ "^/w00tw00t") {
                error 403 "Not permitted";
        }
		## Deliver the content
        return(deliver);
}

## Deliver
sub vcl_deliver {
		## We'll be hiding some headers added by Varnish. We want to make sure people are not seeing we're using Varnish.
              ## Since we're not caching (yet), why bother telling people we use it?
        remove resp.http.X-Varnish;
        remove resp.http.Via;
        remove resp.http.Age;
		
		## We'd like to hide the X-Powered-By headers. Nobody has to know we can run PHP and have version xyz of it.
        remove resp.http.X-Powered-By;
}

Varnishi konfiguratsiooni muutmine

Apache2 tuleb kuulama panna localhosti.

nano /etc/apache2/ports.conf

Muuda

 
NameVirtualHost *:80
Listen 80

selliseks

NameVirtualHost *:8000
Listen 192.168.56.101:8000

192.168.56.101 asemel kasuta oma IP.

Paigaldame apachele lisamooduli, et kuvataks kliendi õige ip aadress.

apt-get install libapache2-mod-rpaf

Deemonite restart

Kõigepealt tuleb apachele restart teha

/etc/init.d/apache2 restart

Kontrollime IP porti

netstat -lp | grep apache2 

Tulemus peab olema taoline:

tcp        0      0 localhost:8000          *:*                     LISTEN      4586/apache2 

Teeme restarti Varnishile

/etc/init.d/varnish restart 

Kontrollime uuesti

netstat -lp | grep varnish 

Tulemus peab olema taoline:

tcp        0      0 *:www                   *:*                     LISTEN      4498/varnishd
tcp6       0      0 [::]:www                [::]:*                  LISTEN      4498/varnishd 

Nüüd on varnish paigaldatud apache2 ette. (Wordpress) sait peab nüüd töötama. Kui apache peatada kuvatakse varnish error lehekülg.

Juhend, et Apache logides näidataks kliendi IP aadressi, mitte Varnishi serveri enda

Varnishi pool

Esimese asjana tuleks muuta Varnishi conf fail

nano /etc/varnish/mysite.vcl

ja lisada sinna

sub vcl_pipe {
set bereq.http.connection = "close";

if (req.http.X-Forwarded-For) {
set bereq.http.X-Forwarded-For = req.http.X-Forwarded-For;
} else {
set bereq.http.X-Forwarded-For = regsub(client.ip, ":.*", "");
}
}

sub vcl_pass {
set bereq.http.connection = "close";


if (req.http.X-Forwarded-For) {
set bereq.http.X-Forwarded-For = req.http.X-Forwarded-For;
} else {
set bereq.http.X-Forwarded-For = regsub(client.ip, ":.*", "");
}
}

Taaskäivita Varnish

service varnish restart

Apache pool

Lisada vajalikud moodulid Apache jaoks

apt-get update && apt-get install libapache2-mod-rpaf && a2enmod rpaf && apache2ctl graceful

Avada Apache conf fail

nano /etc/apache2/apache2.conf

ja lisada sinna see

<IfModule mod_rpaf.c>
RPAFenable On
RPAFsethostname On
RPAFproxy_ips 127.0.0.1 xxx.xxx.xxx.xxx
</IfModule>

Lisa Varnishi serveri IP xxx.xxx.xxx.xxx asemele

Apachele restart

service apache2 restart

Tulemuse kontroll

Ühenda kliendiga veebiserveri külge ja vaata apache access.log faili, mis ip logitakse

less /var/log/apache2/access.log


Kasutatud materjal

1. About Varnish - https://www.varnish-cache.org/about

2. Putting Varnish In Front Of Apache On Ubuntu/Debian - http://www.howtoforge.com/putting-varnish-in-front-of-apache-on-ubuntu-debian

3. Configuring varnish and apache to pass the original client ip http://theyusedtocallitablog.net/2011/07/configuring-varnish-and-apache-to-pass-the-original-client-ip-specifically-with-wordpress-comments-in-mind/