root/trunk/varnish-cache/bin/varnishd/default.vcl

Revision 4467, 4.1 kB (checked in by phk, 3 weeks ago)

Produce the X-Forwarded-For: header in vcl_recv, so people can tweak
as they want.

Append to already existing header if possible.

NB: If you return early from your own vcl_recv, without pasting these
lines in top of your vcl_recv, your backend gets no X-F-F header.

Fixes #601
Fixes #540

  • Property svn:keywords set to Id
Line 
1 /*-
2  * Copyright (c) 2006 Verdens Gang AS
3  * Copyright (c) 2006-2009 Linpro AS
4  * All rights reserved.
5  *
6  * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $Id$
30  *
31  * The default VCL code.
32  *
33  * NB! You do NOT need to copy & paste all of these functions into your
34  * own vcl code, if you do not provide a definition of one of these
35  * functions, the compiler will automatically fall back to the default
36  * code from this file.
37  *
38  * This code will be prefixed with a backend declaration built from the
39  * -b argument.
40  */
41
42 sub vcl_recv {
43     if (req.http.x-forwarded-for) {
44         set req.http.X-Forwarded-For =
45             req.http.X-Forwarded-For ", " client.ip;
46     } else {
47         set req.http.X-Forwarded-For = client.ip;
48     }
49     if (req.request != "GET" &&
50       req.request != "HEAD" &&
51       req.request != "PUT" &&
52       req.request != "POST" &&
53       req.request != "TRACE" &&
54       req.request != "OPTIONS" &&
55       req.request != "DELETE") {
56         /* Non-RFC2616 or CONNECT which is weird. */
57         return (pipe);
58     }
59     if (req.request != "GET" && req.request != "HEAD") {
60         /* We only deal with GET and HEAD by default */
61         return (pass);
62     }
63     if (req.http.Authorization || req.http.Cookie) {
64         /* Not cacheable by default */
65         return (pass);
66     }
67     return (lookup);
68 }
69
70 sub vcl_pipe {
71     # Note that only the first request to the backend will have
72     # X-Forwarded-For set.  If you use X-Forwarded-For and want to
73     # have it set for all requests, make sure to have:
74     # set req.http.connection = "close";
75     # here.  It is not set by default as it might break some broken web
76     # applications, like IIS with NTLM authentication.
77     return (pipe);
78 }
79
80 sub vcl_pass {
81     return (pass);
82 }
83
84 sub vcl_hash {
85     set req.hash += req.url;
86     if (req.http.host) {
87         set req.hash += req.http.host;
88     } else {
89         set req.hash += server.ip;
90     }
91     return (hash);
92 }
93
94 sub vcl_hit {
95     if (!obj.cacheable) {
96         return (pass);
97     }
98     return (deliver);
99 }
100
101 sub vcl_miss {
102     return (fetch);
103 }
104
105 sub vcl_fetch {
106     if (!beresp.cacheable) {
107         return (pass);
108     }
109     if (beresp.http.Set-Cookie) {
110         return (pass);
111     }
112     return (deliver);
113 }
114
115 sub vcl_deliver {
116     return (deliver);
117 }
118
119 sub vcl_error {
120     set obj.http.Content-Type = "text/html; charset=utf-8";
121     synthetic {"
122 <?xml version="1.0" encoding="utf-8"?>
123 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
124  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
125 <html>
126   <head>
127     <title>"} obj.status " " obj.response {"</title>
128   </head>
129   <body>
130     <h1>Error "} obj.status " " obj.response {"</h1>
131     <p>"} obj.response {"</p>
132     <h3>Guru Meditation:</h3>
133     <p>XID: "} req.xid {"</p>
134     <hr>
135     <address>
136        <a href="http://www.varnish-cache.org/">Varnish cache server</a>
137     </address>
138   </body>
139 </html>
140 "};
141     return (deliver);
142 }
Note: See TracBrowser for help on using the browser.