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 ...
随机推荐
- Shell学习笔记 ——第一天
1.程序第一行 指定执行Shell的程序 #!/bin/sh #!用来告诉系统它后面的参数是用来执行该文件的程序 2.在控制台输出信息 echo "Hello Shell" #! ...
- spring bean之间关系
ByeService.java package com.service; public class ByeService { public String sayBye() { return " ...
- js 判断提交表单
<SCRIPT language=javascript> function check_book() { if(document.form1.Username.value=="& ...
- [Android]通过JNI访问并操作Bitmap的元素,支持RGB565和ARGB8888
[Android]通过JNI访问并操作Bitmap的元素,支持RGB565和ARGB8888 标签: androidbitmapjni 2014-05-09 20:35 2985人阅读 评论(1) 收 ...
- cocos2d-x 3.x随机数
1.使用标准库std::rand()函数 rand();//产生0~RAND_MAX(0x7fff=32767)间的int类型随机数 rand()%;//产生0~5间的int类型随机数,不包括5 2. ...
- Callable、Future和FutureTask区别
在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果. 如果需要获取执行结果,就 ...
- robot_framewok自动化测试
robot_framewok自动化测试 http://wenku.baidu.com/view/691abcaa4b73f242336c5fec.html 接口自动化测试框架设计 http://wen ...
- input text设置字体
控件里设置: style="font-family:Arial" html里设置 <font face="Arial">
- sql视图
什么是视图 大家都知道,我们国家现在“神七”上天了.从美国的月球登月开始,人类上天不再是神话.听说,在美国,你只要出几十万美元,您就可以上一次月球进行太空旅行,所以,我们相信:在不久的将来,上天旅行将 ...
- java数据类型,hibernate数据类型,标准sql数据类型之间的对应表
Hibernate API简介 其接口分为以下几类: l 提供访问数据库的操作的接口: l 用于配置Hibernate的接口: l 回调接口 l ...