网站首页/网络技术列表/内容

Varnish设置笔记记录

网络技术2022-07-23阅读
网络技术是从1990年代中期发展起来的新技术,它把互联网上分散的资源融为有机整体,实现资源的全面共享和有机协作,使人们能够透明地使用资源的整体能力并按需获取信息。资源包括高性能计算机、存储资源、数据资源、信息资源、知识资源、专家资源、大型数据库、网络、传感器等。 当前的互联网只限于信息共享,网络则被认为是互联网发展的第三阶段。

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

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

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

  wget

  2、安装

  tar zxf varnish-3.0.3.tar.gz

  cd varnish-3.0.3

  ./autogen.sh

  ./configure --prefix=http://www.3lian.com/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 ~ $ && req.url ~ /static/image/tp.php"

  说明:

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

   为被清除的域名

  /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 ~ "^()?5013.org$") {

  set req.backend = blog;

  } elsif (req.http.host ~ "^()?(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"
    "">
    <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);
    }


网络的神奇作用吸引着越来越多的用户加入其中,正因如此,网络的承受能力也面临着越来越严峻的考验―从硬件上、软件上、所用标准上......,各项技术都需要适时应势,对应发展,这正是网络迅速走向进步的催化剂。

……

相关阅读