虚拟化容器,大数据,DBA,中间件,监控。

Centos 6/7 install varnish v4 代理缓存

28 04月
作者:admin|分类:应用管理

varnish对静态多的网站缓存效果好,对动态多的网站会有各种问题,也有可能出现张冠李戴,登陆退不出的问题,动态多的网站不推荐使用

Centos 6.6/7最小化安装

先导入第三方源

wget http://www.atomicorp.com/installers/atomic #下载,首先使用默认yum源安装wget命令 yum install wget

sh ./atomic #安装

yum clean all #清除当前yum缓存

yum makecache #缓存yum源中的软件包信息

yum repolist #列出yum源中可用的软件包

yum check-update  #更新yum软件包

Install Varnish

yum install varnish

Enable Varnish on Reboot

chkconfig varnish on

Change Apache Listen Port to 8080

vim /etc/httpd/conf/httpd.conf

Listen 80

##change to

Listen 8080

Configure Varnish

vim /etc/sysconfig/varnish

VARNISH_LISTEN_PORT=6081

##change to

VARNISH_LISTEN_PORT=80

Configure Varnish Backend

vim /etc/varnish/default.vcl

backend default {
  .host = "127.0.0.1";
  .port = "80";
}

##change to

backend default {
  .host = "127.0.0.1";
  .port = "8080";
}

Restart Apache and Varnish to enable changes made to configuration

service httpd start
service varnish start

Make sure Varnish and Apache are listening on the correct ports

netstat -plan | grep 80

Should look like:

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      1868/varnishd
tcp        0      0 :::80                       :::*                        LISTEN      1868/varnishd
tcp        0      0 :::8080                     :::*                        LISTEN      1825/httpd
  • You should also be able to verify that Varnish is serving the file:
curl -I http://$IP

HTTP/1.1 200 OK
Server: Apache/2.2.15 (CentOS)
Last-Modified: Tue, 01 Jul 2014 16:33:52 GMT
ETag: "41cf0-5-4fd2457515364"
Content-Type: text/plain; charset=UTF-8
Content-Length: 5
Accept-Ranges: bytes
Date: Tue, 01 Jul 2014 16:36:35 GMT
X-Varnish: 677655568 677655567
Age: 53
Via: 1.1 varnish
Connection: keep-alive

在大部分centos系统,varnish都不能正常启动,一般是因为varnish没有启动脚本,参考下面解决办法

Centos 6.X

Varnish starts if I do this:

bash -x /etc/init.d/varnish start             # 这样是可以启动的

/etc/init.d/varnish start      #这样是不能启动的,还得看下面

[root@6svprx01 ~]# head -1 /etc/init.d/varnish

#! /bin/sh

[root@6svprx01 ~]# rpm -q varnish

varnish-4.0.3-4.el6.art.x86_64

[root@6svprx01 ~]# md5sum /etc/init.d/varnish a69b3b55a8d5da5b9b9c24a62774aa80 /etc/init.d/varnish

[root@6svprx01 ~]# service varnish

Usage: /etc/init.d/varnish {start|stop|status|restart|condrestart|try-restart|reload|force-reload}

[root@6svprx01 ~]# service varnish status

varnishd (pid 6188) is running…

Centos 7

[root@CentOS7 ~]# varnishd -f /etc/varnish/default.vcl -s malloc,1G -T 127.0.0.1:2000 -a 0.0.0.0:8080

不出错,就已经运行了

[root@CentOS7 ~]# curl -I http://127.0.0.1
HTTP/1.1 403 Forbidden
Date: Tue, 04 Aug 2015 07:47:10 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Thu, 16 Oct 2014 13:20:58 GMT
ETag: “1321-5058a1e728280”
Accept-Ranges: bytes
Content-Length: 4897
Content-Type: text/html; charset=UTF-8
X-Varnish: 98322
Age: 0
Via: 1.1 varnish-v4
Connection: keep-alive

当通过访问varnish 80端口和httpd 8080端口得到的页面一样时,说明varnish运行正常

 

参考官方文档:

http://wiki.mikejung.biz/Varnish#Install_Varnish_4.0.2_On_CentOS_6.5

浏览1961 评论0
返回
目录
返回
首页
CentOS 7 yum install Zabbix 2.4.7 rsync + inotify-tools实时文件同步备份/推荐中小网站采用