CentOS 6.4 x64 Varnish 安装配置
Varnish的官方网址为http://varnish-cache.org
首先下载Varnish 稳定版本3.0.3
wget http://repo.varnish-cache.org/source/varnish-3.0.3.tar.gz
Varnish 2+ 版本以上的需要安装 pcre 支持正则规则
tar zxvf pcre-8.10.tar.gz
cd pcre-8.10/
./configure
make
make install
准备完成以后,下面先创建Varnish 运行用户
useradd -s /sbin/nologin varnish
mkdir -p /opt/local/varnish/cache
mkdir -p /opt/local/varnish/log
chown -R varnish:varnish /opt/local/varnish/cache
chown -R varnish:varnish /opt/local/varnish/log
接下来..安装 Varnish
tar zxvf varnish-3.0.3.tar.gz
cd varnish-3.0.3
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
./configure -prefix=/opt/local/varnish --enable-debugging-symbols --enable-extra-developer-warnings --enable-
dependency-tracking
make
make install
安装完毕!!
配置Varnish
cd /opt/local/varnish/etc/varnish
mv default.vcl default.vclbak
vim default.vcl
---------------------------------------------------------------------------------------
backend default {
.host = "10.6.0.10";
.port = "80";
.connect_timeout = 4s;
.first_byte_timeout = 5s;
.between_bytes_timeout = 20s;
}
acl purge {
"localhost";
"127.0.0.1";
}
sub vcl_recv {
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
return(lookup);
}
if (req.http.host ~ "^(.*)?.qq.com$") {
set req.backend = default;
if (req.request != "GET" && req.request != "HEAD") {
return(pipe);
}
else {
return(lookup);
}
}
else {
error 404 " Cache Server";
return(lookup);
}
if (req.request == "GET" && req.url ~ "\.(png|swf|txt|png|gif|jpg|css|js|htm|html)$") {
unset req.http.cookie;
}
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 {
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 {
set resp.http.x-hits = obj.hits ;
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT qq.com";
}
else {
set resp.http.X-Cache = "MISS qq.com";
}
}
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
./varnishd -n /opt/local/varnish/cache -f /etc/varnish/default.vcl -s file,/opt/local/varnish/cache/cache.data,5G -u varnish -w 2,65535,60 -T 127.0.0.1:2002 -a 0.0.0.0:80 -f /opt/local/varnish/etc/varnish/default.vcl -p thread_pool_min=200 -p thread_pool_max=4000 -p thread_pools=4 -p thread_pool_add_delay=2 -p listen_depth=4096 -p lru_interval=20
-s malloc,2G
-s选项用来确定Varnish使用的存储类型和存储容量,这里使用的是malloc类型(malloc是一个C函数,用于分配内存空间),2G 定义多
少内存被malloced。
-T 127.0.0.1:2000
Varnish基于文本方式的一个管理接口,启动后可以在不停止Varnish的情况下来管理Varnish。管理端口2000可以指定。因为不是任何人
都可以访问Varnish管理端口,所以这里推荐只监听本机端口。
-a 0.0.0.0:80
-a选项表示Varnish监听所有IP发给80端口的HTTP请求。
-f /opt/local/varnish/etc/varnish/default.vcl
-f 指定varnish的配置文件
varnishncsa -n /opt/local/varnish/cache
varnishncsa用来将Varnish访问日志写入日志文件
-F
在后台运行
-b address:port
命令用于指定后台服务器地址及其端口
-d
使用debug模式
-P file
指定PID文件
-p param=value
服务器参数,用来优化性能
-s kind[,storageoptions]
缓存内容存放方式
-s file
使用文件做为缓存,其路径、大小等。
==================================启动日志=======================================
./varnishncsa -n /opt/local/varnish/cache/ -w /opt/local/varnish/log/varnish.log &
每天日志分割脚本
---------------------------------------------------------------------------------
#!/bin/sh
logs_path=/opt/local/varnish/log
vlog=${logs_path}/varnish.log
date=$(date -d "yesterday" +"%Y-%m-%d")
pkill -9 varnishncsa
mkdir -p ${logs_path}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv /opt/local/varnish/log/varnish.log ${logs_path}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/varnish-${date}.log
/opt/local/varnish/bin/varnishncsa -n /opt/local/varnish/cache/ -w /opt/local/varnish/log/varnish.log &
----------------------------------------------------------------------------------
crontab -e 添加切割时间
----------------------------------------------------------------------------------
清除部分缓存
/opt/local/varnish/bin/varnishadm -T 127.0.0.1:2002 purge "req.http.host ~ www.qq.com$ && req.url ~ /static/image/1.jpg"
清除 www.qq.com/static/image/1.jpg 这个url 的缓存
清除所有缓存
/opt/local/varnish/bin/varnishadm -T 127.0.0.1:2002 url.purge *$
-----------------------------------------------------------------------------------
查看连接数与命中率
/opt/local/varnish/bin/varnishstat -n /opt/local/varnish/cache/
-n 为 .vsm 文件路径
------------------------------------------------------------------------------------
=======================================================================================
管理工具 Varnishtop 介绍
这个工具用于读取共享内存的日志,适当使用一些过滤选项如-I,-i,-X和-x,可以连续不断地显示大部分普通日志。Varnishtop可以按
照使用要求显示请求的内容、客户端、浏览器等一些其他日志里的信息。
比如:
使用varnishtop -i rxurl查看客户端请求的url次数;
使用Varnishtop -i txurl查看请求后端服务器的url次数;
使用Varnishtop -i Rxheader -I Accept-Encoding查看接收到的头信息中有多少次包含
管理工具 Varnishhist 介绍
用于读取Varnishd共享内存段的日志,并生成一个连续的柱状图。Varnishhist用于显示最后N个请求的处理情况。如果缓存命中则标
记"|",如果缓存没有命中则标记"#"符号。
管理工具 Varnishstat 介绍
用于查看Varnish计数丢失率、命中率、存储信息、创建线程、删除对象等。
=======================================================================================
PS: 1. HTML页面的http头信息中常带有no-cache头,不缓存的问题。
修改Varnish配置文件,要去掉http头信息中的里no-cache头:
----------------------------------------------------------------
sub vcl_fetch {
if (req.url ~ "html$") {
set beresp.ttl = 10m;
set beresp.do_gzip = true;
unset beresp.http.Cache-Control;
unset beresp.http.Pragma;
set beresp.http.Cache-Control = "max-age=60";
unset beresp.http.Expires;
}
}
-----------------------------------------------------------------
如果html页面带有cookie,还需要在sub vcl_recv { } 配置中添加如下内容:
----------------------------------------------------------------
sub vcl_recv {
if (req.request == "GET" && req.url ~ "\.(js|css|html|jpg|png|gif|swf|jpeg| ico)$") {
unset req.http.cookie;
}
}
----------------------------------------------------------------
- Centos 6.5 x64环境下 spark 1.6 maven 编译-- 已验证
Centos 6.5 x64 jdk 1.7 scala 2.10 maven 3.3.3 cd spark-1.6 export MAVEN_OPTS="-Xmx2g -XX:MaxPer ...
- Centos 6.5 X64 环境下编译 hadoop 2.6.0 --已验证
Centos 6.5 x64 hadoop 2.6.0 jdk 1.7 protobuf-2.5.0 maven-3.0.5 set environment export JAVA_HOME=/hom ...
- centos 6.5 x64创建并挂载使用iscsi共享磁盘
前景摘要:NFS或iSCSI,哪个更好?文件 vs 块NFS使用文件级别的实施,服务器或存储阵列托管整个文件系统,客户到文件系统上读写文件,可以在阵列端对主存储数据进行重复数据删除.iSCSI和FC则 ...
- CentOS 6.X x64 编译安装 Countly
CentOS 6.X x64 编译安装Countly 安装所需的软件 yum -y install supervisor ImageMagick sendmail 1. 安装 node.js wge ...
- CentOS 5.8 x64 源码安装 samba-3.6.9
环境 CentOS 5.8 X64 wget http://www.samba.org/samba/ftp/stable/samba-3.6.9.tar.gz tar zxvf samb ...
- CentOS 6.6 x64安装TensorFlow
CentOS 6.6 x64安装TensorFlow升级Python到2.7(系统自带Python版本为2.6) // 安装编译工具 $ yum -y install gcc automake aut ...
- CentOS 6.5 x64 安装Tomcat8 并配置两个Tomcat8
1.首先,安装tomcat的前提是已经配置好jdk环境变量,若没配好可以参考我的上一篇博文:CentOS 6.5 x64安装jdk8,当然也可以通过网络搜索安装步骤~~ 2.下载: 可以通过官网下载: ...
- CentOS 6.5 x64 安装jdk8
1.去官网下载Linux版本的jdk8,我下载的是下面这个 2.下载xftp和xshell来操纵服务器,可以搜索一下下载安装即可,安装完成后,打开xshell,新建链接为你的云服务器的IP地址和密码, ...
- Apache Maven 3.0.3 (yum) 安裝 (CentOS 6.4 x64)
介紹http://maven.apache.org/ Maven是一個專案的開發,管理和綜合工具. 下載http://maven.apache.org/download.cgi 參考http://ma ...
随机推荐
- 通知传值 notification
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.textF = [[UITextField ...
- MyEclipse 2015 如何使项目能够使用 Hibernate自动生成文件
在MyEclipse-Project facets 下 对hibernate这一栏打钩即可
- word project 2010破解
1.用这个工具提示失败: failed to inject memory Failed to inject memory!解决方法 浏览:6545 | 更新:2013-07-15 15:52 在激活o ...
- git push origin master 报错 remote rejected] master -> master (branch is currently checked out)
解决办法: 977down vote You can simply convert your remote repository to bare repository (there is no wor ...
- Htttp协议
我 们在浏览器的地址栏里输入的网站地址叫做URL(UniformResourceLocator,统一资源定位符).就像每家每户都有一个门牌地址一样, 每个网页也都有一个Internet地址.当你在浏览 ...
- Hibernate 系列教程4-单向多对一
项目图片 hibernate.cfg.xml <mapping resource="com/jege/hibernate/one/way/manytoone/User.hbm.xml& ...
- python注意事项
以下基于python3.4.3 1.python3与python2不兼容 2.python语言正确的缩进很重要!事实上缩进是种语法 C中需要 { } 的的地方,python使用 : +缩进 实现 3. ...
- 快学Scala-第八章 继承
知识点: 1.扩展类 extends关键字,在定义中给出子类需要而超类没有的字段和方法,或者重写超类的方法. 2.重写方法 在Scala中重写一个非抽象方法必须 override 修饰符 public ...
- zepto.js 学习之(一)
中文文档:http://mweb.baidu.com/zeptoapi/#attr
- PAT (Advanced Level) 1094. The Largest Generation (25)
简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...