varnish怎么配置

发布时间:2021-08-12 10:47:05 作者:chen
来源:亿速云 阅读:163

本篇内容主要讲解“varnish怎么配置”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“varnish怎么配置”吧!

server1:172.25.254.20  varnish

server2:172.25.254.21  http

主机:172.25.254.44

Varnish是一款高性能的开源HTTP加速器

在server1上:

 #yum install varnish-3.0.5-1.el6.x86_64.rpm  varnish-libs-3.0.5-1.el6.x86_64.rpm -y

 # vim /etc/varnish/default.vcl

backend web1 {

  .host = "127.0.0.1";

  .port = "8080";

}   #定义本机端口

sub vcl_deliver {

if (obj.hits > 0) {

set resp.http.X-Cache = "HIT from westos cache";

}

else {

set resp.http.X-Cache = "MISS from westos cache";

}

return (deliver);

}       #定义是否命中

   #  yum install httpd -y

   #  cd /var/www/html

   # echo server20.example.com > index.html

   #  vim /etc/httpd/conf/httpd.conf

    # Listen 12.34.56.78:80

Listen 8080     #修改http端口,防止和varnish端口冲突

   #  /etc/init.d/httpd start

   #  netstat -antlp   #查看http 8080 端口是否开启

   #  vim /etc/sysconfig/varnish

     VARNISH_LISTEN_PORT=80   # varnish端口

  #  /etc/init.d/varnish start

    [root@server20 html]# iptables -L   

Chain INPUT (policy ACCEPT)

target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)

target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)

target     prot opt source               destination    #查看本机火墙是否关闭

[root@foundation44 Desktop]# curl -I 172.25.254.20

HTTP/1.1 404 westos cache

Server: Varnish

Content-Type: text/html; charset=utf-8

Retry-After: 5

Content-Length: 398

Accept-Ranges: bytes

Date: Wed, 29 Jun 2016 10:11:45 GMT

X-Varnish: 1112141971

Age: 0

Via: 1.1 varnish

Connection: close

X-Cache: MISS from westos cache   #此时未命中,说明还没有缓存

[root@foundation44 Desktop]# curl -I 172.25.254.20

HTTP/1.1 404 westos cach

Server: Varnish

Content-Type: text/html; charset=utf-8

Retry-After: 5

Content-Length: 398

Accept-Ranges: bytes

Date: Wed, 29 Jun 2016 10:15:02 GMT

X-Varnish: 1112141972

Age: 0

Via: 1.1 varnish

Connection: close

X-Cache: HIT from westos cache    #此时命中,说明已经有缓存

   #  vim /etc/varnish/default.vcl

   backend web2 {

   .host = "172.25.254.21";

   .port = "80";

  }

   director lb random  {     #把多个后端聚合在一起

{ .backend = web1;

  .weight = 1;

 }

{ .backend = web2;

  .weight = 2;

}

}

  sub vcl_recv {

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

set req.http.host = "www.westos.org";

set req.backend = lb;

return(pass);            #为了方便,所以定义不缓存

} elsif (req.http.host ~ "^bbs.westos.org") {

set req.backend = web2;

} else {

 error 404 "westos cache";

}

}        #如果访问其他会报错 404

#  /etc/init.d/varnish reload

在server2上进行的操作

 yum install httpd -y

    # cd /var/www/html

    # echo server21.example.com > index.html

    #  /etc/init.d/httpd start

   #  vim /etc/httpd/conf/httpd.conf

NameVirtualHost  *:80

<VirtualHost *:80>

        DocumentRoot /var/www/html

        ServerName www.westos.org

        serveralias westos.org

</VirtualHost>

<VirtualHost *:80>

        DocumentRoot /var/www/virtualhost/bbs

        ServerName bbs.westos.org

</VirtualHost>

   #  mkdir -p  /var/www/virtualhost/bbs

   #  cd /var/www/virtualhost/bbs/

   #  echo bbs.example.com > index.html  

   #  /etc/init.d/httpd restart

   #  vim /etc/httpd/conf/httpd.conf

   # /etc/init.d/httpd restart

然后在主机上加上 server1上和server2上相关的解析,在主机上火狐访问,或者用 curl   -I  www.westos.org

到此,相信大家对“varnish怎么配置”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. 如何在Centos系统中安装配置Varnish
  2. Varnish缓存服务器是什么?工作机制是什么?怎么搭建Varnish?

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

varnish

上一篇:python3中反射的基本方法有哪些

下一篇:C++中引用与const指针与各种传递方式的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》