Database-driven rewrites

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

The module supports MySQL and PostgreSQL databases.

Example usage scenario (for VCL 3.0):

import dbrw;

sub vcl_init { dbrw.config("mysql", "server=db.example.com;database=varnish;user=varnish;password=guessme", "SELECT dest,pattern,value FROM rewrite WHERE host='$host' and '$url' like 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(301, "Page moved"); } }

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

The code for VCL 4.0 (vcl_init remains the same):

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,  "Page moved"));
	}
}

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

Detailed documentation along with more examples is available here .

This module is free software and is available under the terms of GNU General Public License version 3 or (at your option) any later version.

 

Status: 
Used in production
Dependencies: 
mysql or postgres
Licence: 
Varnish version supported: 
Commercial support: 
NXC International