标签:
Varnish可以有效降低web服务器的负载,提升访问速度。按照HTTP协议的处理过程,web服务器接受请求并且返回处理结果,理想情况下服务器要在不做额外处理的情况下,立即返回结果,但实际情况并非如此。当把Varnish部署上之后,web请求的处理过程会有一些变化。客户端的请求将首先被Varnish接受。Varnish将分析接收的请求,并将其转发到后端的web服务器上。后端的web服务器对请求进行常规的处理,并将依次将处理结果返回给Varnish。
但Varnish的功能并非仅限于此。Varnish的核心功能是能能将后端web服务器返回的结果缓存起来,如果发现后续有相同的请求,Varnish将不会将这个请求转发到web服务器,而是返回缓存中的结果。这将有效的降低web服务器的负载,提升响应速度,并且每秒可以响应更多的请求。Varnish速度很快的另一个主要原因是其缓存全部都是放在内存里的,这比放在磁盘上要快的多。诸如此类的优化措施使得Varnish的相应速度超乎想象。但考虑到实际的系统中内存一般是有限的,所以需要手工配置一下缓存的空间限额,同时避免缓存重复的内容。
安装命令
sudo wget http://repo.varnish-cache.org/source/varnish-3.0.3.tar.gz
/usr/local/test/varnish-3.0.3/bin/varnishd/varnishd -f /usr/local/test/varnish-3.0.3/etc/default.vcl
妈个鸡 竟然报错。
原来HTML 分开了行 他就不认识了。 语法跟JAVA拼接字符串一样 FUCK。。
其中,80端口为varnish的代理端口,3500为varnish的管理端口。
2、查看varnish缓存效果与状态
可以通过浏览器访问对应的网页,查看varnish缓存的效果,如果varnish缓存成功的话,第二次打开网页的速度会明显比第一次快,但是这种方式并不能够完全说明问题,下面通过命令行方式,通过查看网页头来查看命中情况,操作如下:
- [root@varnish-server ~]# curl -I http://www.ixdba.net/a/mz/2010/0421/11.html
- HTTP/1.1 200 OK
- Server: Apache/2.2.14 (Unix) PHP/5.3.1 mod_perl/2.0.4 Perl/v5.10.1
- Last-Modified: Sat, 10 Jul 2010 11:25:15 GMT
- ETag: "5e850b-616d-48b06c6031cc0"
- Content-Type: text/html
- Content-Length: 24941
- Date: Fri, 09 Jul 2010 08:29:16 GMT
- X-Varnish: 1364285597
- Age: 0
- Via: 1.1 varnish
- Connection: keep-alive
- X-Cache: MISS from www.ixdba.net
#这里的"MISS"表示此次访问没有从缓存读取。
再次打开这个页面,查看网页头信息:
- [root@varnish-server ~]# curl -I http://www.ixdba.net/a/mz/2010/0421/11.html
- HTTP/1.1 200 OK
- Server: Apache/2.2.14 (Unix) PHP/5.3.1 mod_perl/2.0.4 Perl/v5.10.1
- Last-Modified: Sat, 10 Jul 2010 11:25:15 GMT
- ETag: "5e850b-616d-48b06c6031cc0"
- Content-Type: text/html
- Content-Length: 24941
- Date: Fri, 09 Jul 2010 08:30:35 GMT
- X-Varnish: 1364398612 1364285597
- Age: 79
- Via: 1.1 varnish
- Connection: keep-alive
- X-Cache: HIT from www.ixdba.net
#由“HIT”可知,第二次访问此页面时,是从缓存中读取内容了,也就是缓存命中。
缓存命中率的高低直接说明了varnish的运行状态和效果,较高的缓存命中率说明了varnish运行状态良好,web服务器的性能也会提高很多,反之,过低的缓存命中率说明varnish的配置可能存在问题,那么就需要进行调整,因此,从整体上了解varnish的命中率和缓存状态,对于优化和调整varnish至关重要。
varnish提供了一个varnishstat命令,通过它可以帮我们获得很多重要的信息。
下面是一个varnish系统的缓存状态:
- [root@varnish-server ~]#/usr/local/varnish/bin/varnishstat -n /data/varnish/cache
- Hitrate ratio: 10 100 113
- Hitrate avg: 0.9999 0.9964 0.9964
- 9990 68.92 49.70 Client connections accepted
- 121820 953.84 606.07 Client requests received
- 112801 919.88 561.20 Cache hits
- 68 0.00 0.34 Cache misses
- 2688 33.96 13.37 Backend conn. success
- 6336 1.00 31.52 Backend conn. reuses
- 2642 33.96 13.14 Backend conn. was closed
- 8978 29.96 44.67 Backend conn. recycles
- 6389 1.00 31.79 Fetch with Length
- 2630 32.96 13.08 Fetch chunked
- 444 . . N struct sess_mem
- 23 . . N struct sess
- 64 . . N struct object
- 78 . . N struct objectcore
- 78 . . N struct objecthead
- 132 . . N struct smf
- 2 . . N small free smf
- 3 . . N large free smf
- 6 . . N struct vbe_conn
- 14 . . N worker threads
- 68 1.00 0.34 N worker threads created
- 0 0.00 0.00 N queued work requests
- 1201 11.99 5.98 N overflowed work requests
- 1 . . N backends
- 4 . . N expired objects
- 3701 . . N LRU moved objects
- 118109 942.85 587.61 Objects sent with write
- 9985 71.91 49.68 Total Sessions
- 121820 953.84 606.07 Total Requests
最后这个鸟样。
backend default {
.host = "172.28.5.131";
.port = "3001";
.connect_timeout = 5s;
.first_byte_timeout= 5s;
# .probe = {
# health check
# .url = "/check.txt";
# .interval = 5s;
# .timeout = 5s;# .window = 5;
# .threshold = 3;
# }
}
sub vcl_recv {
# if (req.restarts == 0) {
# if (req.http.x-forwarded-for) {
# set req.http.X-Forwarded-For = req.http.X-Forwarded-For "," client.ip;
# } else {
# set req.http.X-Forwarded-For = client.ip;
# }
# }
if (req.request != "GET" && req.request != "HEAD" && req.request != "PUT" && req.request != "POST" && req.request != "TRACE" && req.request != "OPTIONS" && req.request != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
if (req.http.Authorization || req.http.Cookie) {
/* Not cacheable by default */
return (pass);
}
return (lookup);
}
sub vcl_pipe {
# Note that only the first request to the backend will have
# X-Forwarded-For set. If you use X-Forwarded-For and want to
# have it set for all requests, make sure to have:
# set bereq.http.connection = "close";
# here. It is not set by default as it might break some broken web
# applications, like IIS with NTLM authentication.
return (pipe);
}
sub vcl_pass {
return (pass);
}
sub vcl_hash {
# set req.hash += req.url;
# if (req.http.host) {
# set req.hash += req.http.host;
# } else {
# set req.hash += server.ip;
# }
# return (hash);
}
sub vcl_hit {
# if (!obj.cacheable) {
# return (pass);
# }
return (deliver);
}
sub vcl_miss {
return (fetch);
}
sub vcl_fetch {
# if (!beresp.cacheable) {
# return (pass);
# }
# if (beresp.http.Set-Cookie) {
# return (pass);
# }
return (deliver);
}
sub vcl_deliver { set resp.http.x-cache=obj.hits; return (deliver); }
# 显示x_cache 用。
sub vcl_error {
# set obj.http.Content-Type = "text/html; charset=utf-8";
# synthetic {
# "<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html><head><title>"
# }
#
# obj.status " " obj.response {
# "</title></head><body><h1>Error "
# }
#
# obj.status " " obj.response {
# "</h1><p>"
# }
#
# obj.response {
# "</p><h3>Guru Meditation:</h3><p>XID: "
# }
# req.xid {
# "</p><hr><p>Varnish cache server</p></body></html>"
# };
return (deliver);
}
wget -O varnishd https://raw.github.com/gist/3671408/3a51578bbd60a4cf8317bdc9508527b81eb23da5/varnishdcp varnishd /etc/init.d/varnishdchmod +x /etc/init.d/varnishd/etc/init.d/varnishd start
ps -ef|grep varnish
标签:
原文地址:http://www.cnblogs.com/rocky24/p/a07ef67ef093c646a563c25b92b80e14.html