首发于http://www.linux-ch.com/post-12.html
在这个小小的vps上,从一个小小的emlog变成了emlog+ucenter+dz在上面奔跑的场面,是多么的壮观呀~
但是在实际中发现并不是那么的乐观:内存太小,后台的处理能力大打折扣;后台处理能力不高,却全是动态网站;由于是动态网站,所以更无法同时承受更多用户的访问........
在 硬件不会升级的情况下,我尝试了给php装上eaccelerator和memcache,效果并不明显,或者是我没有配置好的原因,nginx在后台依 旧很频繁是调用php-fpm.在这个时候我发现了一些曙光:让数据更少地在后台的进程间传输,然后直接传输给客户端.后在网友��剑的介绍下尝试了 varnish这个代理软件.
软件下载:wget http://www.varnish-software.com/sites/default/files/varnish-2.1.3.tar.gz
为软件创建用户和存储数据的目录
解压,编译,安装
修改配置文件
vim /usr/local/varnish/etc/varnish/default.vcl
backend linuxch {
.host = "127.0.0.1";
.port = "8000";
.connect_timeout = 1s;
.first_byte_timeout = 5s;
.between_bytes_timeout = 2s;
}
acl purge {
"127.0.0.1";
}
sub vcl_recv {
set req.grace = 30s;
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) {
return (pass);
}
if (req.http.host ~ "^(.*).linux-ch.com") {
set req.backend = linuxch;
}
else {
error 404 "Unknown HostName!";
}
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not Allowed.";
return (lookup);
}
}
if (req.url ~ "\.(php)($|\?)") {
return (pass);
}
if (req.http.Cache-Control ~ "(no-cache|max-age=0)") {
purge_url(req.url);
}
return (lookup);
}
sub vcl_pipe {
return (pipe);
}
sub vcl_pass {
return (pass);
}
sub vcl_hash {
set req.hash += req.url;
if (req.http.host) {
set req.hash += req.http.host;
} else {
set req.hash += server.ip;
}
return (hash);
}
sub vcl_hit {
if (!obj.cacheable) {
return (pass);
}
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged.";
}
return (deliver);
}
sub vcl_miss {
if (req.request == "PURGE") {
error 404 "Not in cache.";
}
return (fetch);
}
sub vcl_fetch {
if (!beresp.cacheable) {
return (pass);
}
if (!(req.url ~ "(admin|login)")) {
remove beresp.http.Set-Cookie;
}
if (req.request == "GET" && req.url ~ "\.(png|swf|txt|png|gif|jpg|jpeg|ico)$") {
unset req.http.cookie;
set beresp.ttl = 10d;
}
elseif (req.request == "GET" && req.url ~ "\.(html|htm|js|css)$") {
set beresp.ttl = 3600s;
else {
set beresp.ttl = 120s;
}
return (deliver);
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
return (deliver)
}
sub vcl_error {
set obj.http.Content-Type = "text/html; charset=utf-8";
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);
}
简单说明下这个配置文件,一开始是定义后端服务器,是在本机上的,端口是8000
然后是管理列表acl,再下面的vcl_recv和 vcl_fetch是用来处理相关请求的.我这里的设置是主机配置"^(.*).linux-ch.com"的交后端,所有php和php?的url也直 接交给后端.在vcl_fetch中定义,所有的png|swf|txt|png|gif|jpg|jpeg|ico文件将在varnish中保留10 天,同时向后端请求的时候会删除没有带有admin|login这些关键字符的以外URL的所有cookie头,这些分别是emlog和dz的登录认证相关的url. html|htm|js|css文件将保留1个小时,其它的保留两分钟.
在2.1后的版本里,原"obj.*"的变量全部变为"beresp.*"了,需要留意一下
然后修改nginx的监听端口至8000,伪静态输出
rewrite ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2 last;
rewrite ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3 last;
rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^(.*)/profile-(username|uid)-(.+)\.html$ $1/viewpro.php?$2=$3 last;
rewrite ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3 last;
rewrite ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2 last;
break;
启动varnishd
为方便管理,利用官方的一个脚本,将varnish添加到系统服务里,并随机启动
vim /etc/rc.d/init.d/varnish
给脚本执行权限,添加服务,修改启动等级
varnish的配置调用文件.是用来告诉程序从哪里读取配置文件,启动参数有哪些等
vim /usr/local/varnish/etc/varnish/varnish
# Configuration file for varnish
#
# /etc/init.d/varnish expects the variable $DAEMON_OPTS to be set from this
# shell script fragment.
#
# Maximum number of open files (for ulimit -n)
NFILES=131072
# Locked shared memory (for ulimit -l)
# Default log size is 82MB + header
MEMLOCK=82000
## Alternative 2, Configuration with VCL
DAEMON_OPTS="-a 74.82.172.143:80 \
-f /usr/local/varnish/etc/varnish/default.vcl \
-T localhost:6082 \
-u varnish -g varnish \
-n /var/vcache \
-s file,/var/vcache/varnish_storage.bin,64M"
到此,整个配置就已经弄好了,整个网站的数据拓扑如下:
client
↓↑ ↑
varnish
↓ ↑
nginx
↓ ↑
php-fpm
如果varnish上面有缓存,将直接传递给用户端,没有的再向后方要,再传递给用户,同时符合存储条件的将保留下来,直至过期.从测试的效果来看,还是比较理想的,实际情况,还得看观察一段时间~