Varnish is a proxy server for caching outputs of the web application. Offers high rate of configurability and much higher performance and lower memory usage. After correct configuration, the server is able to handle many more visitors.
Varnish is deployed on the virtual server and requests on the port 80. It offers already preinstalled HTML requests, that minimize the number of requests that the server has to allocate memory and CPU.
For hosting management via Server Manager it requires additional configuration after the hosting is added. Suitable is mostly if on the server is only one hosting.
These systems offer direct support for Varnish:
- WordPress
- Multisite WordPress
- Magento
- Mediawiki
- Drupal
- Joomla
We have noticed some problems with deployment of Varnish:
- Moodle
- Prestashop
- SugarCRM
- Koken
Varnish Installation
Check if the hostname of the server is set according to DNS
hostname
hostname f
install the package for Varnish and for additional packages, that it depends on
aptget update
aptget install varnish
It is also appropriate to install manual call of its function the utility curl
aptget install curl
Configurating Varnish
1. Set Apache to port 8080 and Varnish to a port 80
Standart Varnish listens on a port 6081, we change it to 80
nano /etc/default/varnish
locate block
DAEMON_OPTS=”a :6081 \
T localhost:6082
f /etc/varnish/default.vcl \
S /etc/varnish/secret \
and replace it
DAEMON_OPTS=”a :80 \
T localhost:6082 \
f /etc/varnish/default.vcl \
S /etc/varnish/secret \
parameter defines a storage for data and its sige. If you have a server with a low RAM (512 MB, we would recommend to change it to s file.
/var/lib/varnish/$INSTANCE/varnish_storage.bin,96M”
we can also change the parameter START = yes so the Varnish would start after the server restarts.
Before you start Varnish, it is necessary to configure the webserver as well:
nano /etc/apache2/ports.conf
listen 80;
we change for
listen 8080;
It is also necessary to to change the configuration files of Apache2 for hosting(s), which you can do easily for all configuration files with a command:
sed i ‘s/\:80/\:8080/’ /etc/apache2/sitesenabled/*
The management of Varnish is possible to adjust with a file /etc/varnish/default.vcl , the each application is specific and there is no universal setting for all.
The basic elements for Varnish management are HTTP heads generating by the application, webserver or visitor’s web browser. On the internet there are many guides for each application.
We will share a few simple tricks with regards on the often used CMS:
backend default {
.host = “127.0.0.1”;
.port = “8080”;
.first_byte_timeout = 300s;
}
here you can define the standard backend where the requests are directed, in our case it is apache2
If you would use a different one, it is mandatory to flag it as well:
backend google {
.host = “209.85.229.106”; /*www.google.com”;*/
.port = “80”;
}
For manual cleaning of the records in cache you can use request type PURGE on URL that should be cleaned. Which addresses can send this request is defined in a directive acl_purge
acl purge {
“firemnyserver.noip.org”;
“wordpress.domena.tld”;
“domena.tld”;
“localhost”;
}
Then the cleaning of the URL is managed in:
sub vlc_recv{ by adding
if (req.request == “PURGE”) {
if (!client.ip ~ purge) {
}
}
and in
sub vcl_hit {
if (req.request == “PURGE”) {
}
}
sub vcl_miss {
if (req.request == “PURGE”) {
}
}
For correct function of the gzip compression it is necessary to add to sub vcl_recv {
at the beginning
if (req.http.AcceptEncoding) {
if (req.url ~ “\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$”) {
# No point in compressing these
remove req.http.AcceptEncoding;
} else if (req.http.AcceptEncoding ~ “gzip”) {
set req.http.AcceptEncoding = “gzip”;
purge_url(req.url);
error 200 “Purged”;
error 404 “Not in cache”;
} else if (req.http.AcceptEncoding ~ “deflate”) {
set req.http.AcceptEncoding = “deflate”;
} else {
# unknown algorithm
remove req.http.AcceptEncoding;
}
}
Because the Varnish is caching only unchanged files, the change in HTTP heads means that the file will not be cached. CMS usually set quite a lot of heads, but for the static content it is not desired.
if (req.request != “GET” &&
req.request != “HEAD” &&
req.request != “PUT” &&
req.request != “POST” &&
req.request != “TRACE” &&
req.request != “OPTIONS” &&
req.request != “DELETE”) {
/* NonRFC2616 or CONNECT which is weird. */
pipe;
}
Caching only request types GET and POST is reached via:
if (req.request != “GET” && req.request != “HEAD”) {
}
if (req.http.Cookie) {
and special persistent p_* cookies.
/* We only deal with GET and HEAD by default */
pass;
# We only care about the “__ac.*” cookies, used for authentication
if (req.http.Cookie ~ “__ac.*” ) {
}
# Else strip all cookies
remove req.http.Cookie;
}
For stopping caching AJAX requests in WordPress and login attempts:
if (req.http.Cookie ~ “wordpress_logged_in_”) {
return (pipe);
}
# don’t cache for users logged into WP backend
if (req.http.Cookie ~ “wordpress_logged_in_”) {
return (pipe);
}
if (req.url ~ “wp(login|admin)” || req.url ~ “preview=true” || req.url ~ “xmlrpc.php” ) {
return (pipe);
}
# don’t cache ajax requests
if (req.http.XRequestedWith == “XMLHttpRequest”) {
return (pipe);
}
Then it is necessary to restart Apache2 and Varnish:
service apache2 restart
service varnish start