summaryrefslogtreecommitdiffabout
path: root/README
blob: b1771498952db6bc65e3edde0ec01307c9ce52f7 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
* Overview

Vmod-dbrw is a Varnish Cache module implementing database-driven
rewrite rules. Its intended use is for web sites that need an
exceedingly big number of redirect and/or rewrite rules. Vmod-dbrw
allows you to keep all rules in an SQL database of arbitrary
structure, considerably speeding up their handling. Another advantage
of this approach is that rewrite rules stored in a database are easier
to maintain. 

This version supports MySQL databases.  Support for PostgreSQL will be
added soon.

* Example

** Simplest case

The redirection database has the following structure

CREATE TABLE redirects (
  host varchar(255) NOT NULL DEFAULT '',
  url varchar(255) NOT NULL DEFAULT '',
  dest varchar(255) DEFAULT NULL,
  PRIMARY KEY (host,url)
);

VCL code:

import dbrw;

sub vcl_init {
	dbrw.config("mysql", "database=dbname;user=varnish;debug=1",
	    "SELECT dest FROM redirects WHERE host='$host' AND url='$url'");
}

sub vcl_recv {
	set req.http.X-Redirect-To =
		dbrw.rewrite("host=" + req.http.Host + ";" +
		             "url=" + req.url);
	if (req.http.X-Redirect-To) {
		error(750, "Redirect");
	}
}

sub vcl_error {
	if (obj.status == 750) {
	        set obj.http.Location = req.http.X-Redirect-To;
                set obj.status = 301;	
		return (deliver);
	}
}

** More complex case: rewrites

Table structure:

CREATE TABLE rewrite (
  host varchar(255) NOT NULL DEFAULT '',
  url varchar(255) NOT NULL DEFAULT '',
  dest varchar(255) DEFAULT NULL,
  value varchar(255) DEFAULT NULL,
  pattern varchar(255) DEFAULT NULL,
  KEY source (host,url)
)

VCL code differs only in definition of the vcl_init:

sub vcl_init {
	dbrw.config("mysql", "database=varnish;user=varnish;debug=10",
		{"SELECT dest,pattern,value FROM rewrite
		  WHERE host='$host' and '$url' like url"});
}

* Installation

In order to compile the package you need to have Varnish source tree.
At least Varnish 3.0.1 is required.  Supposing that the varnish source tree
is available under /usr/src/varnish-3.0.1, run:

  ./configure --with-varnish-source=/usr/src/varnish-3.0.1 --with-vmod-dir

The first option tells configure where Varnish sources reside, the
second one (--with-vmod-dir) instructs it to place created module
files to the standard Varnish module directory.

Once configured, do
  
  make

This will build the module.  After this step you can optionally run
'make test' to test the package.

Finally, do the following command as root:
  
  make install


* Bug reporting

Send bug reports and suggestions to <gray@gnu.org>


Local Variables:
mode: outline
paragraph-separate: "[ 	]*$"
version-control: never
End:

Return to:

Send suggestions and report system problems to the System administrator.