aboutsummaryrefslogtreecommitdiff
path: root/README
blob: 4c22ea4f1dacce70671295ccdfa10d3f82135dd8 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
Vmod-dbrw README
Copyright (C) 2013-2014 Sergey Poznyakoff
See the end of file for copying conditions.

* 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 and PostgreSQL databases.

* 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 3.x 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);
          }
  }

VCL 4.0 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 != "") {
                  return(synth(301, "Redirect"));
         }
  }

  sub vcl_synth {
        if (resp.status == 301) {
                set resp.http.Location = req.http.X-Redirect-To;
                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.
Both Varnish 3.x and 4.x are supported.  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

Once configured, do
  
  make

This will build the module.  Finally, do the following command as root:
  
  make install
  
* Bug reporting

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


* Copyright information:

Copyright (C) 2013-2014 Sergey Poznyakoff

   Permission is granted to anyone to make or distribute verbatim copies
   of this document as received, in any medium, provided that the
   copyright notice and this permission notice are preserved,
   thus giving the recipient permission to redistribute in turn.

   Permission is granted to distribute modified versions
   of this document, or of portions of it,
   under the above conditions, provided also that they
   carry prominent notices stating who last changed them.

Local Variables:
mode: outline
paragraph-separate: "[  ]*$"
version-control: never
End:

Return to:

Send suggestions and report system problems to the System administrator.