varnishcache_rgb-gimp2-alpha

 

What is Varnish?

(From Wikipedia) Varnish is an HTTP accelerator designed for content-heavy dynamic web sites. 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.

My Implementation

I implemented Varnish on this WordPress blog, the technical layout is as follows:

Varish Design

1. Install Varnish on Debian or Ubuntu:

apt-get update
apt-get install varnish

2. Edit /etc/varnish/default.vcl

backend default {
            .host = "localhost";
            .port = "8080";
            .max_connections = 30;
            .connect_timeout = 4.0s;
            .first_byte_timeout = 600s;
            .between_bytes_timeout = 600s;
}
# Drop any cookies sent to WordPress.
sub vcl_recv {
            if (!(req.url ~ "wp-(login|admin)")) {
                       unset req.http.cookie;
            }
}

# Drop any cookies WordPress tries to send back to the client.
sub vcl_fetch {
            if (!(req.url ~ "wp-(login|admin)")) {
                       unset beresp.http.set-cookie;
            }
}

3. Edit /etc/default/varnish

START=yes
NFILES=131072
DAEMON_OPTS="-a :80 
             -T localhost:6082 
             -b localhost:8080 
             -u varnish -g varnish 
             -s malloc,1G"

4. Kill the service

pkill varnishd

5. Edit /etc/apache2/ports.conf and change the listening ports to 8080

NameVirtualHost *:8080
Listen 8080

6. Edit all your vhosts to match the port 8080

Example:

<VirtualHost *:8080>
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/vhosts/example.com
       <Directory /var/www/vhosts/example.com>
               Options Indexes FollowSymLinks MultiViews
               AllowOverride all
       </Directory>
        ErrorLog /var/log/apache2/example.com-error.log
        CustomLog /var/log/apache2/example.com-access.log combined
</VirtualHost>

Default:

[code language="bash"]
<VirtualHost *:8080>
        ServerAdmin [email protected]

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        <</Directory>
        <<Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

7. Launch Varnish

/etc/init.d/apache2 restart
varnishd -f /etc/varnish/default.vcl -s malloc,1G -T 127.0.0.1:2000

Should you have any questions, comments or suggestions, please don’t hesitate to comment below. If you like what you have read, please share it on your favourite social media medium.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.