边界服务器运行NGINX和Varnish,这里讲这两个服务器的安装和配置
Nginx安装MAKE
# ./configure –prefix=/usr/local/nginx –with-http_realip_module
# make
# make install
Debian 安装Nginx
#apt-get install nginx
配置nginx.cong
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream dynamic_node {
server 1.1.1.1:80; # 1.1.1.1 是主DNS节点的IP地址
}
server {
listen 8080;
server_name cdn.xianglei.net;
location ~* \.(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|htm|html|js|css|mp3|swf|ico|flv)$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://dynamic_node;
proxy_store /var/www/cache$uri;
proxy_store_access user:rw group:rw all:r;
}
安装Varnish(make)
# tar -xzvf varnish-2.1.2.tar.gz
# ./configure –prefix=/usr/local/varnish
# make
# make install
配置varnish
backend default {
.host = “127.0.0.1″;
.port = “8080″;
}
sub vcl_recv {
if (req.url ~ “\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf)$”) {
return (lookup);
}
}
sub vcl_fetch {
if (req.url ~ “\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf)$”) {
unset obj.http.set-cookie;
}
}
详细安装过程
-
创建www用户和组,以及Varnish缓存文件存放目录(/var/vcache):
/usr/sbin/groupadd www -g 48
/usr/sbin/useradd -u 48 -g www www
mkdir -p /var/vcache
chmod +w /var/vcache
chown -R www:www /var/vcache
-
创建Varnish日志目录(/var/logs/):
mkdir -p /var/logs
chmod +w /var/logs
chown -R www:www /var/logs
-
编译安装varnish:
wget http://blog.s135.com/soft/linux/varnish/varnish-1.1.2.tar.gz
tar zxvf varnish-1.1.2.tar.gz
cd varnish-1.1.2
./configure --prefix=/usr/local/varnish
make && make install
-
创建Varnish配置文件:
vi /usr/local/varnish/vcl.conf
添加下面的内容:
backend myblogserver {
set backend.host = "192.168.0.5";
set backend.port = "80";
}acl purge {
"localhost";
"127.0.0.1";
"192.168.1.0"/24;
}sub vcl_recv {
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
lookup;
}if (req.http.host ~ "^blog.s135.com") {
set req.backend = myblogserver;
if (req.request != "GET" && req.request != "HEAD") {
pipe;
}
else {
lookup;
}
}
else {
error 404 "Zhang Yan Cache Server";
lookup;
}
}sub vcl_hit {
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged.";
}
}sub vcl_miss {
if (req.request == "PURGE") {
error 404 "Not in cache.";
}
}sub vcl_fetch {
if (req.request == "GET" && req.url ~ "\.(txt|js)$") {
set obj.ttl = 3600s;
}
else {
set obj.ttl = 30d;
}
}
这段配置的意思:
(1)、Varnish通过反向代理请求后端IP为192.168.0.5,端口为80的web服务器;
(2)、Varnish允许localhost、127.0.0.1、192.168.0.***三个来源IP通过PURGE方法清除缓存;
(3)、Varnish对域名为blog.s135.com的请求进行处理,非blog.s135.com域名的请求则返回“Zhang Yan Cache Server”;
(4)、Varnish对HTTP协议中的GET、HEAD请求进行缓存,对POST请求透过,让其直接访问后端Web服务器。之所以这样配置,是因为POST请求一般是发送数据给服务器的,需要服务器接收、处理,所以不缓存;
(5)、Varnish对以.txt和.js结尾的URL缓存时间设置1小时,对其他的URL缓存时间设置为30天。
特别注意:这里对PHP也进行了代理。 需要不代理的部分PHP
if (req.request != "GET" && req.request != "HEAD") {
pipe;
}
elseif(req.url ~ "\.(php|cgi)($|\?)") {
pass;
}
else {
lookup;
}
-
5、启动Varnish
ulimit -SHn 51200
/usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/vcl.conf -a 0.0.0.0:80 -s file,/var/vcache/varnish_cache.data,1G -g www -u www -w 30000,51200,10 -T 127.0.0.1:3500 -p client_http11=on
-
简单管理-查看命中:
/usr/local/varnish/bin/varnishstat
好了,整体的配置就是这样的一个过程。
最新评论