Varnish配置笔记记录

   Varnish是一个开源的反向代理软件和HTTP加速器,与传统的Squid相比,Varnish具有性能更高、速度更快、管理更方便等诸多优点,很多大型的运营网站都开始尝试用Varnish来替换Squid,这些都促使Varnish迅速发展起来。

  1、准备工作及下载源码包

  yum install -y automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig

  wget http://repo.varnish-cache.org/source/varnish-3.0.3.tar.gz

  2、安装

  tar zxf varnish-3.0.3.tar.gz

  cd varnish-3.0.3

  ./autogen.sh

  ./configure --prefix=/usr/local/varnish

  make && make install

  3、添加Varnishd进程用户www,用户组www,创建/var/vcache目录,使www用户有权限可读写

  groupadd www

  useradd www -g www

  mkdir /home/vcache

  chown -R www:www /home/vcache

  chmod -R 750 /home/vcache

  4、编辑/etc/sysctl.conf 优化几个内核参数

  net.ipv4.tcp_fin_timeout = 30

  net.ipv4.tcp_keepalive_time = 300

  net.ipv4.tcp_syncookies = 1

  net.ipv4.tcp_tw_reuse = 1

  net.ipv4.tcp_tw_recycle = 1

  net.ipv4.ip_local_port_range = 5000 65000

  运行sysctl -p 重新按配置文件设置内核参数

  5、启动Varnishd

  /usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/etc/varnish/varnish.conf -a 0.0.0.0:80 -s file,/home/vcache/varnish_cache.data,100M -w 1024,8192,10 -t 3600 -T 127.0.0.1:3500

  参数说明:

  -u 以什么用运行

  -g 以什么组运行

  -f varnish配置文件

  -a 绑定IP和端口

  -s varnish缓存文件位置与大小

  -w 最小,最大线程和超时时间

  -T varnish管理端口,主要用来清除缓存

  -p client_http11=on 支持http1.1协议

  -P(大P) /usr/local/varnish/var/varnish.pid 指定其进程码文件的位置,实现管理

  6、启动varnishncsa用来将Varnish访问日志写入日志文件:

  /usr/local/varnish/bin/varnishncsa -n /home/vcache -w /var/log/varnish.log &

  7、Varnish 缓存清除

  /usr/local/varnish/bin/varnishadm -T 192.168.1.180:3500 purge "req.http.host ~ www.5013.org$ && req.url ~ /static/image/tp.php"

  说明:

  192.168.1.180:3000 为被清除缓存服务器地址

  www.5013.org 为被清除的域名

  /static/image/tp.php 为被清除的url地址列表

  清除所有缓存

  /usr/local/varnish/bin/varnishadm -T 192.168.1.180:3500 url.purge *$

  清除image目录下所有缓存

  /usr/local/varnish/bin/varnishadm -T 192.168.1.180:3500 url.purge /image/

  8、将加入启动项

  vi /etc/rc.local

  ulimit -SHn 51200

  /usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/etc/varnish/varnish.conf -a 0.0.0.0:80 -s file,/home/vcache/varnish_cache.data,100M -w 1024,8192,10 -t 3600 -T 127.0.0.1:3500

  /usr/local/varnish/bin/varnishncsa -n /home/vcache -w /var/log/varnish.log &

  9、杀掉varnishd进程

  pkill varnishd

  10、查看varnishd命中率

  /usr/local/varnish/bin/varnishstat

  11、更新系统时间

  yum install -y ntp

  ntpdate time.nist.gov

  echo "00 01 * * * ntpdate time.nist.gov" 》 /etc/crontab

  附件多主机多域名varnish.conf 配置

  backend blog {

  .host = "198.56.193.190";

  .port = "80";

  }

  backend www {

  .host = "192.168.1.170";

  .port = "80";

  }

  sub vcl_recv {

  if (req.http.host ~ "^(www.)?5013.org$") {

  set req.backend = blog;

  } elsif (req.http.host ~ "^(www.)?(test1.com|test2.com)$") {

  set req.backend = www;

  } else {

  error 404 "Unknown virtual host";

  }

  }

  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);

  }

 #只有GET与HEAD方法才会使用Lookup,使用缓存。
    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);
    # }
    #如果请求的是php页面直接转发到后端服务器
    if (req.url ~ ".(php|cgi)($|?)") {
    return (pass);
    }
    return (lookup);
    }
    sub vcl_pipe {
    return (pipe);
    }
    sub vcl_pass {
    return (pass);
    }
    sub vcl_hash {
    hash_data(req.url);
    if (req.http.host) {
    hash_data(req.http.host);
    } else {
    hash_data(server.ip);
    }
    return (hash);
    }
    sub vcl_hit {
    return (deliver);
    }
    sub vcl_miss {
    return (fetch);
    }
    sub vcl_fetch {
    if (beresp.ttl <= 0s ||
    beresp.http.Set-Cookie ||
    beresp.http.Vary == "*") {
    /*
    * Mark as "Hit-For-Pass" for the next 2 minutes
    */
    set beresp.ttl = 120 s;
    return (hit_for_pass);
    }
    if (req.url ~ ".(png|gif|jpg)$") {
    unset beresp.http.set-cookie;
    set beresp.ttl = 1h;
    }
    #设置图片的缓存TTL为一小时
    return (deliver);
    }
    sub vcl_deliver {
    return (deliver);
    }
    sub vcl_error {
    set obj.http.Content-Type = "text/html; charset=utf-8";
    set obj.http.Retry-After = "5";
    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);
    }
    sub vcl_init {
    return (ok);
    }
    sub vcl_fini {
    return (ok);
    }

时间: 2024-08-23 11:41:06

Varnish配置笔记记录的相关文章

Redis基本知识、安装、部署、配置笔记

 这篇文章主要介绍了Redis基本知识.安装.部署.配置笔记,本文讲解了基本知识.安装Redis.Redis的配置等知识,需要的朋友可以参考下     基本知识 1.Redis的数据类型: 字符串.列表(lists).集合(sets).有序集合(sorts sets).哈希表(hashs) 2.Redis和memcache相比的独特之处: (1)redis可以用来做存储(storge).而memcache是来做缓存(cache).这个特点主要是因为其有"持久化"功能 (2)存储的数据有

运行Node.js的IIS扩展iisnode安装配置笔记

 这篇文章主要介绍了运行Node.js的IIS扩展iisnode安装配置笔记,iisnode的扩展可以把Node.js程序托管到IIS,托管之后也意味着可以使用IIS里面的各种功能,需要的朋友可以参考下     今年年初打算用Node.js基于Express框架重写博客程序,从此告别ASP.NET.然而,我目前用的VPS是Windows Server系统.IIS服务器,如果让Express和IIS都监听80端口,明显会产生冲突.幸好,有一个叫做iisnode的扩展可以把Node.js程序托管到I

开源堡垒机GateOne的安装、配置笔记(详细步骤)

  GateOne简介 GateOne是一款基于HTML5的开源终端模拟器/SSH客户端,同时内置强大的插件功能.它自带的插件使其成为一款令人惊艳的SSH客户端,但是,它可以用于运行任何终端应用.用户可以将GateOne嵌入其他应用程序从而提供各类终端访问界面,它也支持各类基于Web的管理界面--后面的大家自己看吧~~ 因为内部临时需要这么一套系统,所以搜搜查查,搞定了系统部署,使用pam认证的配置,做了个简单的管理界面. GateOne部署 系统初始化是使用CentOS 6.5 Mini x6

笔记记录软件:NoteRecorder

    利用SQLite数据库做了个笔记记录软件,加上搜索功能,方便自己记录东西所用.下载地址   (请在安装了.net framework 3.0/3.5的机器上运行,win7默认已安装该运行时) 参考资料:   1.KTDictSeg 分词组件: http://www.cnblogs.com/eaglet/archive/2008/11/17/1335098.html   2.ICSharpCode.TextEditor 高亮文本编辑器:http://www.icsharpcode.net/

Nginx + PHP(FastCGI)安装配置笔记

这是站长在2011年时一次安装服务器时所记录的Nginx和PHP的安装笔记.安装过程记录的比较简略,仅仅是一个大致的流程,一些细节描述的不够详细,请多多谅解,我会在日后重新整理一份详细的安装过程,本文仅供参考! 软件环境:CentOS 5.7 + PHP 5.2.17 + Nginx 0.8.55   1. 编译安装 PHP 和 PHP-FPM 提示: 安装PHP前应先安装MySQL,具体方法请自行baidu或google: PHP所需支持库的安装这里不做过多介绍,可上网搜索参照相关教程进行安装

在Win 2003中为Web站点配置DNS记录

概要 本文介绍了如何配置"域名系统"(DNS) 服务器,使其承载可从外部访问(即从Internet 访问)的 Web 站点. 如何获取 IP 地址若要承载可从外部访问的 Web 站点,必须从您的 Internet 服务提供商 (ISP) 那里获取一个公用 IP 地址.并将此 IP 地址指定到 DNS 服务器所连接防火墙或路由器的外部接口 如何注册域名 通过 Internet 域名注册管理机构(这样的管理机构被称为注册机构)为您的组织注册一个父级或二级 DNS 域名.有关全球认可的注册机

hadoop1.1.2+hbase0.92.2+nutch2.2.1+solr4.6.1配置全记录

    花了两天时间,总算把这些东西弄好了,环境是vmware9虚拟机上的Ubuntu12.04_x86版.做了个简单的测试,跑通了.不过还是有很多不明白的地方,后期再陆续记录.     目前楼主配的是单机模式,分布式模式中遇到的问题以后再做记录.     另外强烈的吐槽一下,有些版本的在官网上面都找不到,搜了好久找到了某论坛资源,下载还要积分,郁闷.我想说的是其实所有的版本都可以在这个地址上下载: http://archive.apache.org/dist/ ,那些要积分的真无语,本来这东西

Redis基本知识、安装、部署、配置笔记_基础知识

基本知识 1.Redis的数据类型: 字符串.列表(lists).集合(sets).有序集合(sorts sets).哈希表(hashs) 2.Redis和memcache相比的独特之处: (1)redis可以用来做存储(storge).而memcache是来做缓存(cache).这个特点主要是因为其有"持久化"功能 (2)存储的数据有"结构",对于memcache来说,存储的数据,只有一种类型--"字符串",而redis则可以存储字符串.链表.

阿里云 redhat 5.4下lnmp 配置笔记

redhat-release.x86_64 0:5Server-5.4.0.3   一.挂在数据盘 1.ssh登录 2.fdisk -l 3.硬盘分区 fdisk /dev/xvdb    n    p    w    检查分区fdisk -l 4.硬盘格式化mkfs.ext3 /dev/xvdb1 5.mkdir alidata && mount /dev/xvdb1 /alidata 开机自动挂载: ROOT登陆后 键入命令 vi  /etc/fstab 在最下面加上一条 /dev/x