阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux安装配置Varnish Web加速器

138次阅读
没有评论

共计 4551 个字符,预计需要花费 12 分钟才能阅读完成。

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

一、环境

# cat /etc/issue

CentOS release 6.3 (Final)

Kernel \r on an \m

# getconf LONG_BIT

64

二、下载

cd /usr/local/src/

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

tar xzvf varnish-3.0.1.tar.gz

三、安装

cd varnish-3.0.1

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

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

make

make install

四、校验安装

cd /usr/local/varnish/sbin/

./varnishd -V

五、配置

# cd /usr/local/varnish/etc/varnish/

# cp default.vcl default.vcl.bak

# > default.vcl

# cat default.vcl

# This is a basic VCL configuration file for varnish.  See the vcl(7)

# man page for details on VCL syntax and semantics.

#

# Default backend definition.  Set this to point to your content

# server.

#

 backend default {

    .host = “115.28.225.216”;

    .port = “80”;

    ### 下面三行为新加配

    .connect_timeout = 1s;

    .first_byte_timeout = 5s;

    .between_bytes_timeout = 2s;

 }

#

# 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 {

    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_pipe {

#    # Note that only the first request to the backend will have

#    # X-Forwarded-For set.  If you use X-Forwarded-For and want to

#    # have it set for all requests, make sure to have:

#    # set bereq.http.connection = “close”;

#    # here.  It is not set by default as it might break some broken web

#    # applications, like IIS with NTLM authentication.

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

    }

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

 }

六、启动与关闭 varnish

/usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/default.vcl -s malloc,1024m -T 127.0.0.1:200 -a 0.0.0.0:80

启动参数介绍:
-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,这也是默认的。

pkill varnishd    // 关闭 Varnish

/usr/local/varnish/bin/varnishncsa -w /var/log/varnish.log &    // 启动 varnishncsa 用来将 Varnish 访问日志写入日志文件 ;

————————————————————

Varnish Cache 的架构笔记 http://www.linuxidc.com/Linux/2013-10/91016.htm

CentOS 5.8 下 Varnish-2.1.5 的安装配置 http://www.linuxidc.com/Linux/2013-09/89916.htm

RedHat 脚本改用 CentOS 源更新安装 Nginx、PHP 5.3、Varnish http://www.linuxidc.com/Linux/2012-07/65801.htm

利用 Varnish 构建 Cache 服务器笔记 http://www.linuxidc.com/Linux/2012-07/65234.htm

缓存服务 Varnish 安装配置 http://www.linuxidc.com/Linux/2012-07/65228.htm

Varnish 编译安装所需准备 http://www.linuxidc.com/Linux/2012-07/65230.htm

Linux 下 Varnish 缓存的配置优化 http://www.linuxidc.com/Linux/2012-03/56435.htm

Varnish 基础概念详解 http://www.linuxidc.com/Linux/2014-07/104535.htm

———————————————————————————–

Varnish 的详细介绍 :请点这里
Varnish 的下载地址 :请点这里

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计4551字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中