varnish下使用acl限制ip地址访问

第1步:定义ACL,我们使用一个外部文件存储IP地址

  1. acl forbidden {
  2. include "/etc/varnish/chinaip.dat";
  3. }
  4. ############chinaip.data#########
  5.  
  6. "192.168.1.0"/24;
  7. "10.0.0.0"/24;

 

第2步:在vcl_recv中定义策略,放到最前面。

  1. if (client.ip ~ forbidden) {
  2. error 505 "Forbidden";
  3. }

 

第3步(可选):自定义错误页面
#根据不同的错误代码,执行不同的操作
#将错误代码为750的,重定向google,将错误代码为505的,直接返回错误代码。

  1. sub vcl_error {
  2. set obj.http.Content-Type = "text/html; charset=utf-8";
  3. if (obj.status == 750) {
  4. set obj.http.Location = "http://www.google.com/";
  5. set obj.status = 302;
  6. deliver;
  7. }
  8. else {
  9. synthetic {"
  10. <!–?xml version="1.0" encoding="utf-8"?–>
  11.  
  12.  
  13.  
  14. "} obj.status " " obj.response {"
  15. <h1>Error "} obj.status " " obj.response {"</h1>
  16. "} obj.response {"
  17.  
  18. "};
  19. }
  20.  
  21. return (deliver);
  22. }

 

第4步:验证配置是否正确

  1. varnishd -d -f /etc/varnish/my.vcl

 

第5步:重启varnish

  1. service varnish restart

 

第6步:测试

忘记说第0步了,就是先备份你的配置文件,很重要。
转自:http://blog.poesylife.com/

如何用DNS+GeoIP+Nginx+Varnish做世界级的CDN

如何用BIND, GeoIP, Nginx, Varnish来创建你自己的高效的CDN网络?
CDN,意思是Content Distrubtion Network,意思是内容分发网络,简单的说,就是全地域范围内的负载均衡,全地域的概念可以是全国,也可以是全世界。由统一的DNS服务器进行地址转发,选择离用户最近的地区服务器进行负载均衡。本质上是从一个机房内的负载均衡扩展到了全世界范围内的负载均衡。同时可以将本地化的内容,由当地的服务器实现。做浏览器的地区自动选择。
比如在中国,被人为划分成两大区域,北方是网通,南方是电信。这两个网络之间互访是比较慢的。作为大型网站,一种解决办法是将全部服务器架设在双线或三线ISP处,由ISP来提供路由上的选择。这样做,线路的成本会比较高。另一种办法就是将服务器架设在两边,南方一台,北方一台,然后由服务器自己选择,如果IP在电信,就转发请求到南方的服务器,如果是网通就转发到北方的服务器。
再扩大范围,可以将美国来的请求交由美国服务器处理,这样也缩短了用户在路由上的等待时间。这就是内容分发网络。
而作为这个网络上的所有节点,都可以当成虚拟服务器来看待。至于在各地的服务器如何做负载均衡,可以由各节点之间完成。
准备工作如下:你需要下载如下软件以实现上述功能
NginxBINDGeoIPVarnish
接下来是编译和安装bind9和geoip

  1. # tar -xzvf bind-9.2.4.tar.gz
  2. # tar -xzvf GeoIP-1.4.6.tar.gz
  3. # cd GeoIP-1.4.6
  4. # ./configure –prefix=/usr/local/geoip
  5. # make
  6. # make install
  7. # cd ..
  8. # patch -p0 < bind-9.2.4-geodns-patch/patch.diff //给bind9打补丁,让bind9直接支持geoip库
  9. # cd bind-9.2.4
  10. # CFLAGS=”-I/usr/local/geoip/include” LDFLAGS=”-L/usr/local/geoip/lib -lGeoIP” ./configure –prefix=/usr/local/bind
  11. # make
  12. # make install

装好bind后我们来制作named.conf

  1. view “us” {
  2. // 匹配北美的客户端 US & Canada
  3. match-clients { country_US; country_CA; };
  4. // Provide recursive service to internal clients only.
  5. recursion no;
  6. zone “cdn.xianglei.com” {
  7. type master;
  8. file “pri/xianglei-us.db”;
  9. };
  10. zone “.” IN {
  11. type hint;
  12. file “named.ca”;
  13. };
  14. };
  15. view “latin” {
  16. // 匹配到南美国家
  17. match-clients { country_AR; country_CL; country_BR; };
  18. recursion no;
  19. zone “cdn.xianglei.com” {
  20. type master;
  21. file “pri/xianglei-latin.db”;
  22. };
  23. zone “.” IN {
  24. type hint;
  25. file “named.ca”;
  26. };
  27. };

照此办理,你也可以匹配到欧洲,非洲等等,然后来开始制作nginx和varnish
注意,以上内容是你要在主节点服务器上做的,主节点服务器只负责对DNS请求进行转发。
约定一下,我们将Bind服务器叫做动态节点服务器,Nginx+Varnish叫做边界服务器。
以下内容是副节点服务器需要做的,也就是实际在某个地区放置的服务器

  1. # ./configure –prefix=/usr/local/nginx –with-http_realip_module
  2. # make
  3. # make install

并配置Nginx

  1. http {
  2. include mime.types;
  3. default_type application/octet-stream;
  4. sendfile on;
  5. keepalive_timeout 65;
  6. upstream dynamic_node {
  7. server 1.1.1.1:80; # 1.1.1.1 是主DNS节点的IP地址
  8. }
  9. server {
  10. listen 8080;
  11. server_name cdn.xianglei.net;
  12. location ~* .(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|htm|html|js|css|mp3|swf|ico|flv)$ {
  13. proxy_set_header X-Real-IP $remote_addr;
  14. proxy_pass http://dynamic_node;
  15. proxy_store /var/www/cache$uri;
  16. proxy_store_access user:rw group:rw all:r;
  17. }

以上我们用nginx只对静态文件进行缓存,将静态文件缓存在/var/www/cache文件夹下,如果你没有的话,需要创建这个文件夹。并且nginx监听的是8080端口,这是因为,我们需要用varnish来监听80端口进行动态文件的转发。这里实际上是用nginx做了一个静态文件的反向代理和缓存的服务器,而真正让用户能够看到网页和动态文件的反向代理是varnish,而静态和动态文件的分开存放,能将效率提升不少。
最后我们来配置varnish服务。

  1. # tar -xzvf varnish-2.1.2.tar.gz
  2. # ./configure –prefix=/usr/local/varnish
  3. # make
  4. # make install

然后是varnish的选项

  1. backend default {
  2. .host = “127.0.0.1″;
  3. .port = “8080″;
  4. }
  5. sub vcl_recv {
  6. if (req.url ~ “.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf)$”) {
  7. return (lookup);
  8. }
  9. }
  10. sub vcl_fetch {
  11. if (req.url ~ “.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf)$”) {
  12. unset obj.http.set-cookie;
  13. }
  14. }

其他的配置内容可参看varnish的配置文章。
总结:
这样做的好处在于:
1.从根源上解决了DNS在轮询上的不确定性,能够做到在DNS上的快速响应。也避免了过去用Nginx+GeoIP时的负载高的问题。毕竟DNS的计算要比Nginx小多了。
2.降低大网站的服务器负载压力和运营成本,毕竟F5BigIP和双线路的价格和服务费都太高了。
3.易扩展性强,如某地区负载压力大,只需在该地区增加边界服务器组的web server即可完成,无需考虑跳转问题。
其他优点我再想想。
来源:http://slaytanic.blog.51cto.com/2057708/516093

varnish前端缓存测试

今天给lamp一键安装包增加了一个扩展-varnish,主要作为wordpress博客的缓存。下面我们来用ab工具做一个简单的并发为100,连接数为1000的压力测试,一个是没有启用varnish缓存的,一个是varnish前面缓存的。
服务器参数:系统centos 5 32位,单核,Intel(R) Xeon(R) CPU L5630 @ 2.13GHz,1G内存
没有经过varnish缓存的测试结果:
负载40.44
Concurrency Level: 100
Time taken for tests: 305.766 seconds
Complete requests: 1000
Failed requests: 1
(Connect: 0, Receive: 0, Length: 1, Exceptions: 0)
Write errors: 0
Non-2xx responses: 1
Total transferred: 19746015 bytes
HTML transferred: 19521638 bytes
Requests per second: 3.27 [#/sec] (mean)
Time per request: 30576.620 [ms] (mean)
Time per request: 305.766 [ms] (mean, across all concurrent requests)
Transfer rate: 63.07 [Kbytes/sec] received
开启了varnish前端缓存的
负载0.05

Concurrency Level: 100
Time taken for tests: 12.720 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 20086736 bytes
HTML transferred: 19821261 bytes
Requests per second: 78.62 [#/sec] (mean)
Time per request: 1271.991 [ms] (mean)
Time per request: 12.720 [ms] (mean, across all concurrent requests)
Transfer rate: 1542.15 [Kbytes/sec] received

这两组数据不用我说,你也应该明白了。

Linux安装配置varnish web加速器

Varnish是一款高性能的开源HTTP加速器,它可以来做纯粹的代理服务器,负载均衡,但varnish最主要的功能是缓存加速,也是它最出色的地方。下面介绍如何安装和使用。

  1. wget -c http://repo.varnish-cache.org/source/varnish-3.0.1.tar.gz
  2. tar xzvf varnish-3.0.1.tar.gz
  3. cd varnish-3.0.1
  4. ./configure –prefix=/usr/local/varnish
  5. make
  6. make install
  7. groupadd varnish
  8. useradd -d /var/lib/varnish -g varnish -s /sbin/nologin varnish
  9. ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/varnishd

启动varnish:

  1. varnishd -f /usr/local/varnish/etc/varnish/default.vcl -s malloc,1G -g varnish -u varnish -T 127.0.0.1:2000

关闭varnish:

  1. pkill varnish

启动参数介绍:
-f /usr/local/etc/varnish/default.vcl
这个 –f 选项指定varnishd使用哪个配置文件。
-s malloc,1G
这个 –s 选项用来确定varnish使用的存储类型和存储容量,我使用的是malloc类型(malloc是一个C函数,用于分配内存空间), 1G 定义多少内存被malloced,1G = 1gigabyte。
-T 127.0.0.1:2000
Varnish有一个基于文本的管理接口,启动它的话可以在不停止varnish的情况下来管理varnish。您可以指定管理软件监听哪个接口。当然您不能让全世界的人都能访问您的varnish管理接口,因为他们可以很轻松的通过访问varnish管理接口来获得您的root访问权限。我推荐只让它监听本机端口。如果您的系统里有您不完全信任的用户,您可以通过防火墙规则来限制他访问varnish的管理端口。
-a 0.0.0.0:8080
这一句的意思是制定varnish监听所有IP发给8080端口的http请求,如果在生产环境下,您应该让varnish监听80,这也是默认的。
vcl配置文件的介绍请执行如何命令查看:
man /usr/local/varnish/share/man/man7/vcl.7
也可以查看在线文档:https://www.varnish-cache.org/docs/3.0/

wordpress varnish vcl配置文件

如需使用varnish作wordpress缓存,已经开发有一键包http://devops.webres.wang/lampv/
使用如下配置文件,建议安装wordpress插件Varnish HTTP Purge来自动清除缓存,此配置文件实现的功能如下:
1、varnish作为前端,使用80端口
2、允许127.0.0.1和devops.webres.wang清除缓存
3、在缓存之前,删除常见静态文件的cookie。
4、http.x-forwarded-for获取真实IP。
5、不缓存wordpress后台页面,不缓存已登录的用户和保留评论者cookie。
6、后端服务器状态检查,如发生故障,继续以旧缓存内容服务。
下面是配置文件内容:

  1. backend wp1
  2. {
  3. .host = “127.0.0.1”;
  4. .port = “8000”;
  5. .probe = {
  6.                 .url = “/”;
  7.                 .timeout = 1s;
  8.                 .interval = 60s;
  9.                 .window = 1;
  10.                 .threshold = 1;
  11.         }
  12. }
  13. acl purge {
  14. “127.0.0.1”;
  15. “devops.webres.wang”;
  16. }
  17. sub vcl_recv {
  18.           if (req.request == “PURGE”) {
  19.                 if (!client.ip ~ purge) {
  20.                         error 405 “Not allowed.”;
  21.                 }
  22.                 return (lookup);
  23.         }
  24. }
  25. # Do the PURGE thing
  26. sub vcl_hit {
  27.         if (req.request == “PURGE”) {
  28.                 purge;
  29.                 error 200 “Purged.”;
  30.         }
  31. }
  32.  
  33. sub vcl_miss {
  34.         if (req.request == “PURGE”) {
  35.                 purge;
  36.                 error 200 “Purged.”;
  37.         }
  38. }
  39. sub vcl_recv {
  40.     ## always cache these images & static assets
  41.     if (req.request == “GET” && req.url ~ “.(css|js|gif|jpg|jpeg|bmp|png|ico|img|tga|wmf|html|htm)$”) {
  42.       remove req.http.cookie;
  43.       return(lookup);
  44.     }
  45.     if (req.request == “GET” && req.url ~ “(xmlrpc.php|wlmanifest.xml)”) {
  46.       remove req.http.cookie;
  47.       return(lookup);
  48.     }
  49. ## get real ip address
  50. if (req.http.x-forwarded-for) {
  51.  set req.http.X-Forwarded-For =
  52.  req.http.X-Forwarded-For + “, “+ client.ip;
  53.  } else {
  54.  set req.http.X-Forwarded-For = client.ip;
  55.  }
  56.     ##never cache POST requests
  57.     if (req.request == “POST”)
  58.     {
  59.       set req.backend = wp1;
  60.       return(pass);
  61.     }
  62.  
  63.     ### do not cache these files:
  64.     ##never cache the admin pages, or the server-status page
  65.     if (req.request == “GET” && (req.url ~ “(wp-admin|wp-login|server-status)”))
  66.     {
  67.       return(pipe);
  68.     }
  69.  
  70.     #DO cache this ajax request
  71.     if(req.http.X-Requested-With == “XMLHttpRequest” && req.url ~ “recent_reviews”)
  72.     {
  73.       return (lookup);
  74.     }
  75.  
  76.     #dont cache ajax requests
  77.     if(req.http.X-Requested-With == “XMLHttpRequest” || req.url ~ “nocache” || req.url ~ “(control.php|wp-comments-post.php|wp-login.php|bb-login.php|bb-reset-password.php|register.php)”)
  78.     {
  79.         return (pass);
  80.     }
  81.  
  82.     if (req.http.Cookie && req.http.Cookie ~ “wordpress_”) {
  83.         set req.http.Cookie = regsuball(req.http.Cookie, “wordpress_test_cookie=”, “; wpjunk=”);
  84.     }
  85.     ### don’t cache authenticated sessions
  86.     if (req.http.Cookie && req.http.Cookie ~ “(wordpress_|PHPSESSID|comment_author_)”) {
  87.         return(pass);
  88.     }
  89.  
  90.     ### parse accept encoding rulesets to make it look nice
  91.     if (req.http.Accept-Encoding) {
  92.         if (req.http.Accept-Encoding ~ “gzip”) {
  93.         set req.http.Accept-Encoding = “gzip”;
  94.         } elsif (req.http.Accept-Encoding ~ “deflate”) {
  95.         set req.http.Accept-Encoding = “deflate”;
  96.         } else {
  97.         # unkown algorithm
  98.         remove req.http.Accept-Encoding;
  99.         }
  100.     }
  101. if (req.backend.healthy) {
  102.                 set req.grace = 120s; /* Only enable if you don’t mind slightly stale content */
  103.         } else {
  104.                 set req.grace = 24h;
  105.         }
  106.  
  107.     return(lookup);
  108. }
  109. sub vcl_fetch {
  110. set beresp.grace = 24h; /* Keep at longest used in vcl_recv */
  111. set beresp.ttl = 1h;
  112. }