Varnish前端代理Drupal的配置以及Jenkins

本文介绍了如何配置Varnish作为Drupal的前端代理,并设置了Jenkins的代理转发。此外,还提供了Varnish配置文件示例及一些常用命令,如测试VCL、清除缓存等。

Varnish前端代理Drupal的配置以及Jenkins

鉴于Varnish使用以及教程都较少,所以特将Varnish作为drupal的前端代理配置贴出来,供大家参考使用。顺便设置了Jenkins的代理转发,如果要启动Jenkins,请注意端口号。

Apache/Nginx只需监听127.0.0.1的80端口即可,Varnish监听公网IP的80端口,负责转发请求。
文件中附带了Round-Robin负载均衡的设置,如果有多个服务器,可以参考之,文件默认位置:/etc/varnish/default.vcl。

backend default {
  .host = "127.0.0.1";
  .port = "80";
}
 
backend jenkins {
  .host = "127.0.0.1";
  .port = "8100";
}
 
##############################
##   Round Robin Backend     #
##############################
#backend rr1 {
#  .host = "192.168.8.1";
#  .port = "80";
#  .probe = { .url = "/ee.txt";
#             .interval = 20 s;
#             .timeout = 10 s;
#             .window = 10;
#             .threshold = 2;
#  }
#}
#backend rr2 {
#  .host  = "192.168.8.2";
#  .port  = "80";
#  .probe = { .url = "/ee.txt";
#             .interval = 20 s;
#             .timeout = 10 s;
#             .window = 10;
#             .threshold = 2;
#  }
#}
#director zcn round-robin {
#  { 
#    .backend = rr1;
#  }
#  { 
#    .backend = rr2;
#  }
#}
##############################
 
# 
# Below is a commented-out copy of the default VCL logic.  If you
# redefine any of these subroutines, the built-in logic will be
# appended to your code.
sub vcl_recv {
 
  #jenkins
  if (req.url ~ "^/jenkins") {
    set req.backend = jenkins;
  }
 
  #if (req.http.host ~ "^domain.com$") {
  # set req.backend = default;
  #}
 
  # Download large files
  if (req.url ~ "\.(csv|pdf|xml|rar|zip)") {
     return(pipe);
  }
  # Prevent cache
  if (req.url ~ "^logout$" || req.url ~ "^/cron.php") {
     return (pass);
  }
 
  # Remove cookies
  set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(_gscu_|_gscs_|_gscbrs_|zinch_flag_like|zt|paq_cookietime)[^;]*", "");
  set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
  if (req.http.Cookie ~ "^\s*$") {
    remove req.http.Cookie;
  }
  if (req.url ~ "\.(jpg|png|css|js|ico|gz|tgz|bz2|tbz|gif)$") {
    remove req.http.cookie;
  }
  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_deliver {
  # Add http header to indicate if cache hit
  if (obj.hits > 0) {
    set resp.http.X-Cache = "HIT";
  } else {
    set resp.http.X-Cache = "MISS";
  }
}

顺便公布几个Varnish常用的技巧命令:

测试VCL

/usr/sbin/varnishd -F -f /etc/varnish/active.vcl -u varnish -g varnish -a :81 -n test_instance -s file,/tmp/varnish_storage.bin,10M

清除所有缓存

ban.url .*

载入新VCL以及使用

vcl.load  load01 /etc/varnish/new.vcl;
vcl.use load01

其他参考文章:
Varnish构建高负载Drupal网站 – 高级篇
高负载网站之Varnish与Drupal – 基本篇


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值