This tutorial is intended for system administrators wanting to on 6. The reader should know how to configure a web or application and have basic knowledge of the HTTP protocol. Once finished the reader should have a basic Varnish up and running with the default configuration.

Varnish is a web application accelerator. You install it in front of your web application and it will speed it up significantly.

Varnish web application accelerator homepage: https://www.varnish-cache.org

Install the Varnish Repository

# -Uvh http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release-3.0-1.noarch.

Install Varnish web accelerator

# yum install varnish

Enable Varnish web accelerator at startup

# chkconfig varnish on

Basic default.vcl

# vi /etc/varnish/default.vcl

1
2
3
4
5
6
7
8
9
10
# This is a basic VCL configuration file for varnish.  See the vcl(7)
# man page for details on VCL syntax and semantics.
#
# Default backend definition.  Set this to point to your content
# server.
#
backend default {
  .host = "127.0.0.1";
  .port = "80";
}

Start Varnish web accelerator

# service varnish start

You will now have a basic Varnish web accelerator running on port 8080

Top 5 Varnish commands

varnishstat

Provides all the info you need to spot cache misses and errors.

varnishhist

Provides a histogram view of cache hits/misses

varnishlog

Provides detailed information on requests.

varnishtop

The varnishtop utility reads varnishd shared memory logs and presents a continuously updated list of the most commonly occurring log entries.

varnishadm

Command-line varnish administration used to reload vcl and purge urls.

This is only the manifest and doesn’t include any of the files (i.e. default.vcl).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class varnish::repo {

	Package {
		provider => rpm,
		ensure => installed
	}

	package { "varnish-release": source => "http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release-3.0-1.noarch.rpm"
	}
}

class varnish::install {

	$packagelist = ["varnish"]

	package { $packagelist:
		require => Class ["varnish::repo"],
		ensure => installed
	}
}

class varnish::service {

	service { "varnish":
		ensure => true,
		enable => true,
		hasrestart => true,
		hasstatus => true,
		require => Class ["varnish::install"]
	}
}

class varnish::conf {

	File {
		require => Class ["varnish::install"],
		owner => "root",
		group => "root",
		mode => 644,
		notify => Class ["varnish::service"]
	}

	file { "/etc/varnish/default.vcl":
		source  => "puppet:///modules/varnish/default.vcl"
	}

	file { "/etc/sysconfig/varnish":
		source  => "puppet:///modules/varnish/varnish"
	}
}

class varnish {
	include varnish::repo, varnish::install, varnish::service, varnish::conf
}

 

/etc/varnish/default.vcl

Varnish uses VCL files to route requests and determine how to cache and how long. VCL is a subset of the C programming language and is actually compiled into computer language when loaded. In essence your VCLs drive Varnish as if you programmed it, making the routing very efficient. For your convenience I’ve included a simple VCL for WordPress which does the following for you:

  • Setup a single backend (your backend is basically Apache on the same machine, but advanced setups can include loadbalancing)
  • Allow purge requests from localhost (for your WordPress plugins)
  • Strip cookies from all requests except login, wp-admin, comments
  • Disable caching of the admin interface

I’ve also included links to more advanced VCL setups which include more functionality. Keep in mind that this VCL is tailored to WordPress and static sites, but does not include provisions for other setups. I’ve taken this from the Varnish documentation and tweaked it slightly

You can download it here:

default.vcl (please don’t forget to modify the default backend to point to apache)

/etc/sysconfig/varnish

This file setups your Varnish preferences allowing you to customize your settings through a few different “Alternatives”. I tend to stick with the default enabled one: Alternative 3.

The included configuration sets the following for you:

  • Varnish uses port 8080
  • Admin IP is 127.0.0.1 and port 2000
  • Varnish min threads 64 (spin up some threads to do work)
  • Varnish storage size to 1G
  • Varnish storage mode malloc (meaning in memory instead of to file)
Print Friendly

Comments

comments

Bài viết liên quan