虚拟化容器,大数据,DBA,中间件,监控。

用varnish来加速图片服务器

09 03月
作者:admin|分类:系统运维

varnish是和squid类似的高性能开源HTTP加速器,我这里用来缓存图片,js,css等小文件

varnish cache 192.168.0.15 centos6.0
nagios www后端 192.168.0.11 centos5.3

1.安装varnish

  1. wget http://repo.varnish-cache.org/source/varnish-3.0.0.tar.gz
  2. tar zxvf varnish-3.0.0.tar.gz
  3. cd varnish-3.0.0
  4. ./configure --prefix=/opt/varnish-3.0.0
  5. make
  6. make install
  7. ln -s /opt/varnish-3.0.0 /opt/varnish

2.设置权限

  1. cd /opt/varnish
  2. #varnish以www:website来运行
  3. chown -R www:website /opt/varnish/var/varnish/
  4. mkdir /var/log/varnish
  5. chown -R www:website /var/log/varnish
  6. chown -R www:website /opt/varnish/var/varnish/`hostname`
  7. mkdir /opt/varnish/var/varnish/`hostname`

3.配置文件

#查看默认配置文件
cat etc/varnish/default.vcl

#编辑新配置文件
vi etc/vcl.conf

  1. #http请求处理过程
  2. #1,receive请求入口状态,根据vcl判断pass还是lookup本地查询
  3. #lookup,在hash表中查找数据,若找到则进入hit状态,否则进入fetch状态
  4. #pass,选择后台,进入fetch状态
  5. #fetch,对请求进行后端的获取,发送请求,获得数据,并进行本地存储
  6. #deliver,将数据发送给客户端,进入done
  7. #done,处理结束
  8. backend wwwserver {
  9.        .host = "192.168.0.11";
  10.        .port = "80";
  11. }
  12. backend staticserver {
  13.   .host = "192.168.0.11";
  14.   .port = "80";
  15. }
  16. acl purge {
  17.        "localhost";
  18.        "127.0.0.1";
  19.        "192.168.1.0"/24;
  20. }
  21.  
  22. sub vcl_recv {
  23.        if (req.request == "PURGE") {
  24.                if (!client.ip ~ purge) {
  25.                        error 405 "Not allowed.";
  26.                }
  27.                return(lookup);
  28.        }
  29. #去除cookie
  30. if (req.request == "GET" && req.url ~ "^/[^?]+\.(jpeg|jpg|png|gif|ico|swf|js|css|txt|zip|html|htm)(\?.*|)$") {
  31. unset req.http.Cookie;
  32. }
  33. #判断req.http.X-Forwarded-For 如果前端有多重反向代理,这样可以获取客户端IP地址。
  34. if (req.http.x-forwarded-for)
  35. {
  36. set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", "+ client.ip;
  37. }
  38.         else {
  39. set req.http.X-Forwarded-For = client.ip;
  40. }
  41.  
  42. #浏览器Accept-Encoding兼容
  43. if (req.http.Accept-Encoding) {
  44. if (req.url ~ "\.(jpg|png|gif|jpeg)$") {
  45.     remove req.http.Accept-Encoding;
  46. } elsif (req.http.Accept-Encoding ~ "gzip") {
  47.     set req.http.Accept-Encoding = "gzip";
  48. } elsif (req.http.Accept-Encoding ~ "deflate") {
  49.     set req.http.Accept-Encoding = "deflate";
  50. } else {
  51.     remove req.http.Accept-Encoding;
  52. }
  53. }
浏览1954 评论0
返回
目录
返回
首页
优化Apache服务器的重负荷 让Windows Server 2008 + IIS 7+ ASP.NET 支持10万个同时请求