用varnish来加速图片服务器
varnish是和squid类似的高性能开源HTTP加速器,我这里用来缓存图片,js,css等小文件
varnish cache 192.168.0.15 centos6.0
nagios www后端 192.168.0.11 centos5.3
1.安装varnish
- wget http://repo.varnish-cache.org/source/varnish-3.0.0.tar.gz
- tar zxvf varnish-3.0.0.tar.gz
- cd varnish-3.0.0
- ./configure --prefix=/opt/varnish-3.0.0
- make
- make install
- ln -s /opt/varnish-3.0.0 /opt/varnish
2.设置权限
- cd /opt/varnish
- #varnish以www:website来运行
- chown -R www:website /opt/varnish/var/varnish/
- mkdir /var/log/varnish
- chown -R www:website /var/log/varnish
- chown -R www:website /opt/varnish/var/varnish/`hostname`
- mkdir /opt/varnish/var/varnish/`hostname`
3.配置文件
#查看默认配置文件
cat etc/varnish/default.vcl
#编辑新配置文件
vi etc/vcl.conf
- #http请求处理过程
- #1,receive请求入口状态,根据vcl判断pass还是lookup本地查询
- #lookup,在hash表中查找数据,若找到则进入hit状态,否则进入fetch状态
- #pass,选择后台,进入fetch状态
- #fetch,对请求进行后端的获取,发送请求,获得数据,并进行本地存储
- #deliver,将数据发送给客户端,进入done
- #done,处理结束
- backend wwwserver {
- .host = "192.168.0.11";
- .port = "80";
- }
- backend staticserver {
- .host = "192.168.0.11";
- .port = "80";
- }
- acl purge {
- "localhost";
- "127.0.0.1";
- "192.168.1.0"/24;
- }
- sub vcl_recv {
- if (req.request == "PURGE") {
- if (!client.ip ~ purge) {
- error 405 "Not allowed.";
- }
- return(lookup);
- }
- #去除cookie
- if (req.request == "GET" && req.url ~ "^/[^?]+\.(jpeg|jpg|png|gif|ico|swf|js|css|txt|zip|html|htm)(\?.*|)$") {
- unset req.http.Cookie;
- }
- #判断req.http.X-Forwarded-For 如果前端有多重反向代理,这样可以获取客户端IP地址。
- 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;
- }
- #浏览器Accept-Encoding兼容
- if (req.http.Accept-Encoding) {
- if (req.url ~ "\.(jpg|png|gif|jpeg)$") {
- remove req.http.Accept-Encoding;
- } elsif (req.http.Accept-Encoding ~ "gzip") {
- set req.http.Accept-Encoding = "gzip";
- } elsif (req.http.Accept-Encoding ~ "deflate") {
- set req.http.Accept-Encoding = "deflate";
- } else {
- remove req.http.Accept-Encoding;
- }
- }
目录 返回
首页