码迷,mamicode.com
首页 > 其他好文 > 详细

varnish代理缓存服务器的安装与使用

时间:2017-11-04 13:32:13      阅读:423      评论:0      收藏:0      [点我收藏+]

标签:分享   sbin   apach   截图   转发   实验   .net   多个   reg   

1. 下载解压
cd /usr/local/src/
wget https://codeload.github.com/varnishcache/varnish-cache/zip/master
chmod 775 varnish-cache-master.zip 

unzip varnish-cache-master.zip 

varnish-cache-master.zip 


2. 安装
cd varnish-cache-master
chmod -R 755 *
yum install autoconf automake jemalloc-devel libedit-devel libtool ncurses-devel pcre-devel pkgconfig python-docutils python-sphinx
./autogen.sh
./configure --prefix=/usr/local/varnish PKG_CONFIG_PATH=/usr/lib/pkgconfig
make
make install


3. 配置
cd /usr/local/varnish/
mkdir /var/varnish_cache
mkdir etc
vi etc/web.conf ##详见 web.conf 文件内容
vi /etc/init.d/varnish ##详见 varnish 文件内容
chmod -R 755 /etc/init.d/varnish


4. 启动项
service iptables stop
chkconfig iptables --level 2345 off
chkconfig --add varnish
chkconfig varnish --level 016 on


5. 修改 httpd 的配置
vi /alidata/server/httpd/conf/httpd.conf
Listen8080  ##必须与varnish监听的端口一致


vi /alidata/server/httpd/conf/vhosts/leizhiman.conf 
<VirtualHost *:8080>  ##必须与httpd.conf的端口一致
        DocumentRoot /alidata/www/leizhiman
        ServerName leizhiman.cn
        ServerAlias www.leizhiman.cn
        <Directory "/alidata/www/leizhiman">
            Options FollowSymLinks
            AllowOverride all
            Order allow,deny
            Allow from all
        </Directory>
        ErrorLog "/alidata/log/httpd/leizhiman-error.log"
        CustomLog "/alidata/log/httpd/leizhiman.log" common

</VirtualHost>


service httpd restart

service varnish start

 

6.测试: 访问 http://www.leizhiman.cn ( 注意: 第一次访问是 varnish-nocache )

技术分享


实验成功!!


----------------------------------------------------以下是配置文件---------------------------------------------------------


web.conf 文件内容


技术分享
  1 # This is a Varnish 4.x VCL file
  2 vcl 4.0;
  3 
  4 backend default {
  5     .host = "127.0.0.1";
  6     .port = "8080";  ##必须跟 httpd.conf 的端口一致
  7     .probe = {
  8         .url = "/ping";
  9         .timeout   = 1s;
 10         .interval  = 10s;
 11         .window    = 5;
 12         .threshold = 2;
 13     }
 14     .first_byte_timeout     = 300s;   # How long to wait before we receive a first byte from our backend?
 15     .connect_timeout        = 5s;     # How long to wait for a backend connection?
 16     .between_bytes_timeout  = 2s;     # How long to wait between bytes received from our backend?
 17 }
 18 
 19 backend web1 {
 20     .host = "127.0.0.1";
 21     .port = "8080"; ##必须跟 httpd.conf 的端口一致
 22 }
 23 ##如果有多个, 就继续加: 比如 web2
 24 
 25 # Below is an example redirector based on round-robin requests
 26 import directors;
 27 sub vcl_init {
 28     new cluster1 = directors.round_robin();
 29     cluster1.add_backend(web1);    ##必须对应 backend web1 中的 "web1"
 30 
 31     #cluster1.add_backend(web2);
 32 }
 33 
 34 
 35 acl purge {
 36     # For now, Ill only allow purges coming from localhost
 37     "127.0.0.1";
 38     "localhost";
 39 }
 40 
 41 # Handle the HTTP request received by the client
 42 sub vcl_recv {
 43     # Choose the round-robin backend
 44     #set req.backend_hint = cluster1.backend();
 45 
 46     # Or chose the client-IP backend (sticky sessions)
 47     #set req.backend_hint = cluster2.backend();
 48 
 49     # shortcut for DFind requests
 50     if (req.url ~ "^/w00tw00t") {
 51         return (synth(404, "Not Found"));
 52     }
 53 
 54     if (req.restarts == 0) {
 55         if (req.http.X-Forwarded-For) {
 56             set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
 57         } else {
 58             set req.http.X-Forwarded-For = client.ip;
 59         }
 60     }
 61 
 62     # Normalize the header, remove the port (in case youre testing this on various TCP ports)
 63     #set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");
 64     if (req.http.host ~ "(?i)^(www.)?leizhiman.cn$") {  ##根据域名转发到指定后端服务器
 65         set req.backend_hint = cluster1.backend();
 66     }
 67 
 68     # Allow purging
 69     if (req.method == "PURGE") {
 70         if (!client.ip ~ purge) {
 71             # Not from an allowed IP? Then die with an error.
 72             return (synth(405, "This IP is not allowed to send PURGE requests."));
 73         }
 74 
 75         # If you got this stage (and didnt error out above), purge the cached result
 76         return (purge);
 77     }
 78 
 79     # Only deal with "normal" types
 80     if (req.method != "GET" &&
 81             req.method != "HEAD" &&
 82             req.method != "PUT" &&
 83             req.method != "POST" &&
 84             req.method != "TRACE" &&
 85             req.method != "OPTIONS" &&
 86             req.method != "PATCH" &&
 87             req.method != "DELETE") {
 88         /* Non-RFC2616 or CONNECT which is weird. */
 89         return (pipe);
 90     }
 91 
 92     # Only cache GET or HEAD requests. This makes sure the POST requests are always passed.
 93     if (req.method != "GET" && req.method != "HEAD") {
 94         return (pass);
 95     }
 96 
 97     # Configure grace period, in case the backend goes down. This allows otherwise "outdated"
 98     # cache entries to still be served to the user, because the backend is unavailable to refresh them.
 99     # This may not be desireable for you, but showing a Varnish Guru Meditation error probably isnt either.
100     #set req.grace = 15s;
101     #if (std.healthy(req.backend)) {
102     #    set req.grace = 30s;
103     #} else {
104     #    unset req.http.Cookie;
105     #    set req.grace = 6h;
106     #}
107 
108     # Some generic URL manipulation, useful for all templates that follow
109     # First remove the Google Analytics added parameters, useless for our backend
110     if (req.url ~ "(\?|&)(utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl)=") {
111         set req.url = regsuball(req.url, "&(utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl)=([A-z0-9_\-\.%25]+)", "");
112         set req.url = regsuball(req.url, "\?(utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl)=([A-z0-9_\-\.%25]+)", "?");
113         set req.url = regsub(req.url, "\?&", "?");
114         set req.url = regsub(req.url, "\?$", "");
115     }
116 
117     # Strip hash, server doesnt need it.
118     if (req.url ~ "\#") {
119         set req.url = regsub(req.url, "\#.*$", "");
120     }
121 
122     # Strip a trailing ? if it exists
123     if (req.url ~ "\?$") {
124         set req.url = regsub(req.url, "\?$", "");
125     }
126 
127     # Some generic cookie manipulation, useful for all templates that follow
128     # Remove the "has_js" cookie
129     set req.http.Cookie = regsuball(req.http.Cookie, "has_js=[^;]+(; )?", "");
130 
131     # Remove any Google Analytics based cookies
132     set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(; )?", "");
133     set req.http.Cookie = regsuball(req.http.Cookie, "_ga=[^;]+(; )?", "");
134     set req.http.Cookie = regsuball(req.http.Cookie, "utmctr=[^;]+(; )?", "");
135     set req.http.Cookie = regsuball(req.http.Cookie, "utmcmd.=[^;]+(; )?", "");
136     set req.http.Cookie = regsuball(req.http.Cookie, "utmccn.=[^;]+(; )?", "");
137 
138     # Remove the Quant Capital cookies (added by some plugin, all __qca)
139     set req.http.Cookie = regsuball(req.http.Cookie, "__qc.=[^;]+(; )?", "");
140 
141     # Remove the AddThis cookies
142     set req.http.Cookie = regsuball(req.http.Cookie, "__atuvc=[^;]+(; )?", "");
143 
144     # Remove a ";" prefix in the cookie if present
145     set req.http.Cookie = regsuball(req.http.Cookie, "^;\s*", "");
146 
147     # Are there cookies left with only spaces or that are empty?
148     if (req.http.cookie ~ "^\s*$") {
149         unset req.http.cookie;
150     }
151 
152     # Normalize Accept-Encoding header
153     # straight from the manual: https://www.varnish-cache.org/docs/3.0/tutorial/vary.html
154     if (req.http.Accept-Encoding) {
155         if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
156             # No point in compressing these
157             unset req.http.Accept-Encoding;
158         } elsif (req.http.Accept-Encoding ~ "gzip") {
159             set req.http.Accept-Encoding = "gzip";
160         } elsif (req.http.Accept-Encoding ~ "deflate") {
161             set req.http.Accept-Encoding = "deflate";
162         } else {
163             # unkown algorithm
164             unset req.http.Accept-Encoding;
165         }
166     }
167 
168     # Large static files should be piped, so they are delivered directly to the end-user without
169     # waiting for Varnish to fully read the file first.
170     # TODO: once the Varnish Streaming branch merges with the master branch, use streaming here to avoid locking.
171     if (req.url ~ "^[^?]*\.(mp[34]|rar|tar|tgz|gz|wav|zip)(\?.*)?$") {
172         unset req.http.Cookie;
173         return (pipe);
174     }
175 
176     # Remove all cookies for static files
177     # A valid discussion could be held on this line: do you really need to cache static files that dont cause load? Only if you have memory left.
178     # Sure, theres disk I/O, but chances are your OS will already have these files in their buffers (thus memory).
179     # Before you blindly enable this, have a read here: http://mattiasgeniar.be/2012/11/28/stop-caching-static-files/
180     if (req.url ~ "^[^?]*\.(bmp|bz2|css|doc|eot|flv|gif|gz|ico|jpeg|jpg|js|less|pdf|png|rtf|swf|txt|woff|xml)(\?.*)?$") {
181         unset req.http.Cookie;
182         return (hash);
183     }
184 
185     # Send Surrogate-Capability headers to announce ESI support to backend
186     set req.http.Surrogate-Capability = "key=ESI/1.0";
187 
188 
189     if (req.http.Authorization) {
190         # Not cacheable by default
191         return (pass);
192     }
193 
194     return (hash);
195 }
196 
197 
198 sub vcl_pipe {
199     # Note that only the first request to the backend will have
200     # X-Forwarded-For set.  If you use X-Forwarded-For and want to
201     # have it set for all requests, make sure to have:
202     # set bereq.http.connection = "close";
203     # here.  It is not set by default as it might break some broken web
204     # applications, like IIS with NTLM authentication.
205 
206     #set bereq.http.Connection = "Close";
207     return (pipe);
208 }
209 
210 sub vcl_pass {
211 #    return (pass);
212 }
213 
214 # The data on which the hashing will take place
215 sub vcl_hash {
216     hash_data(req.url);
217 
218     if (req.http.host) {
219         hash_data(req.http.host);
220     } else {
221         hash_data(server.ip);
222     }
223 
224     # hash cookies for requests that have them
225     if (req.http.Cookie) {
226         hash_data(req.http.Cookie);
227     }
228 }
229 
230 sub vcl_hit {
231     return (deliver);
232 }
233 
234 sub vcl_miss {
235     return (fetch);
236 }
237 
238 # Handle the HTTP request coming from our backend
239 sub vcl_backend_response {
240     # Pause ESI request and remove Surrogate-Control header
241     if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
242         unset beresp.http.Surrogate-Control;
243         set beresp.do_esi = true;
244     }
245 
246     # Enable cache for all static files
247     # The same argument as the static caches from above: monitor your cache size, if you get data nuked out of it, consider giving up the static file cache.
248     # Before you blindly enable this, have a read here: http://mattiasgeniar.be/2012/11/28/stop-caching-static-files/
249     if (bereq.url ~ "^[^?]*\.(bmp|bz2|css|doc|eot|flv|gif|gz|ico|jpeg|jpg|js|less|mp[34]|pdf|png|rar|rtf|swf|tar|tgz|txt|wav|woff|xml|zip)(\?.*)?$") {
250         unset beresp.http.set-cookie;
251     }
252 
253     # Sometimes, a 301 or 302 redirect formed via Apaches mod_rewrite can mess with the HTTP port that is being passed along.
254     # This often happens with simple rewrite rules in a scenario where Varnish runs on :80 and Apache on :8080 on the same box.
255     # A redirect can then often redirect the end-user to a URL on :8080, where it should be :80.
256     # This may need finetuning on your setup.
257     #
258     # To prevent accidental replace, we only filter the 301/302 redirects for now.
259     if (beresp.status == 301 || beresp.status == 302) {
260         set beresp.http.Location = regsub(beresp.http.Location, ":[0-9]+", "");
261     }
262 
263     # Set 2min cache if unset for static files
264     if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary == "*") {
265         set beresp.ttl = 120s;
266         set beresp.uncacheable = true;
267         return (deliver);
268     }
269 
270     # Allow stale content, in case the backend goes down.
271     set beresp.grace = 6h;
272 
273     return (deliver);
274 }
275 
276 # The routine when we deliver the HTTP request to the user
277 # Last chance to modify headers that are sent to the client
278 sub vcl_deliver {
279     if (obj.hits > 0) {
280         set resp.http.X-Cache = "varnish-cached"; ##这里的值是浏览器显示的名称 (见测试那一步的截图)
281     } else {
282         set resp.http.x-Cache = "varnish-nocache";
283     }
284 
285     # Remove some headers: PHP version
286     unset resp.http.X-Powered-By;
287 
288     # Remove some headers: Apache version & OS
289     unset resp.http.Server;
290     unset resp.http.X-Drupal-Cache;
291     unset resp.http.X-Varnish;
292     unset resp.http.Via;
293     unset resp.http.Link;
294 
295     return (deliver);
296 }
297 
298 
299 sub vcl_synth {
300     if (resp.status == 720) {
301         # We use this special error status 720 to force redirects with 301 (permanent) redirects
302         # To use this, call the following from anywhere in vcl_recv: error 720 "http://host/new.html"
303         set resp.status = 301;
304         set resp.http.Location = resp.reason;
305         return (deliver);
306     } elseif (resp.status == 721) {
307         # And we use error status 721 to force redirects with a 302 (temporary) redirect
308         # To use this, call the following from anywhere in vcl_recv: error 720 "http://host/new.html"
309         set resp.status = 302;
310         set resp.http.Location = resp.reason;
311         return (deliver);
312     }
313 
314     return (deliver);
315 }
316 
317 sub vcl_init {
318     return (ok);
319 }
320 
321 sub vcl_fini {
322     return (ok);
323 }
View Code

 

varnish 文件内容


技术分享
 1 # chkconfig: 2345 10 90 
 2 # description: varnish ....
 3 #!/bin/sh
 4 
 5 start()
 6 {
 7         echo -n $"starting varnish..."
 8         /usr/local/varnish/sbin/varnishd -P /tmp/varnish.pid -a 0.0.0.0:80 -T 127.0.0.1:3500 -f /usr/local/varnish/etc/web.conf -n /var/varnish_cache -s malloc,1G -P client_http11=on  ##启动命令, 注意配置文件路径: /usr/local/varnish/etc/web.conf 
 9         echo
10 }
11 
12 stop()
13 {
14         echo -n $"stopping varnish..."
15         pkill varnish
16         echo
17 }
18 
19 restart()
20 {
21    stop
22    sleep 2
23    start
24 }
25 
26 case "$1" in
27 start)
28 start
29 ;;
30 stop)
31 stop
32 ;;
33 restart)
34 restart
35 ;;
36 *)
37 echo $"Usage: $0 {start|stop|restart}"
38 esac
View Code

 




 

varnish代理缓存服务器的安装与使用

标签:分享   sbin   apach   截图   转发   实验   .net   多个   reg   

原文地址:http://www.cnblogs.com/upup2015/p/7782431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!