2013-09-03 10:56:56
来 源
ITJS.CN
Varnish
本文介绍使用Varnish,Nginx搭建WEB缓存服务器,希望对于Varnish入门者给与帮助。更多Varnish资源请本站内搜索。

一. varnish

1.安装pcre库,兼容正则表达式

# tar -zxvf pcre-8.10.tar.gz

# cd pcre-8.10

# ./configure --prefix=/usr/local/pcre

# make && make install

2.配置安装varnish

# tar -zxvf varnish-3.0.2.tar.gz

# cd varnish-3.0.2

# export PKG_CONFIG_PATH=/usr/local/pcre/lib/pkgconfig/

# ./configure --prefix=/usr/local/varnish

# make && make install

3.修改varnish配置文件

/usr/local/varnish/etc/varnish/default.vcl

# mv default.vcl default.vcl.bak

# vi cq.vcl

backend cqserver {

.host = "127.0.0.1";

.port = "8087";

.connect_timeout = 20s;

}

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.";

}

return (lookup);

}

if (req.http.host ~ "^www.baidu.com") {

set req.backend = cqserver;

if (req.request != "GET" && req.request != "HEAD") {

return (pipe);

}

else{

return (lookup);

}

}

else {

error 404 "caoqing Cache Server";

return (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.";

}

}

(1)Varnish通过反向代理请求后端IP为127.0.0.1,端口为8087的web服务器,即nginx服务器监听端口;

(2)Varnish允许localhost、127.0.0.1、192.168.1.*三个来源IP通过PURGE方法清除缓存;

(3)Varnish对域名为www.baidu.com的请求进行处理,非www.baidu.com域名的请求则返回"caoqing Cache Server";

(4)Varnish对HTTP协议中的GET、HEAD请求进行缓存,对POST请求透过,让其直接访问后端Web服务器。

4.启动varnish

# cd /usr/local/varnish/sbin/

# ./varnishd -f /usr/local/varnish/etc/varnish/cq.vcl -s file,/var/varnish_cache,1G -T 127.0.0.1:2000 -a 0.0.0.0:80

二. nginx

1.安装nginx

# rpm -ivh zlib-devel-1.2.3-4.el5.i386.rpm

# tar zxvf nginx-1.4.1.tar.gz

# cd nginx-1.4.1

# ./configure --prefix=/usr/local/nginx --with-openssl=/usr/lib --with-pcre=/root/tool/pcre-8.10 --with-http_stub_status_module

# make && make install

2.启动nginx

# cd /usr/local/nginx/sbin

# ./nginx

3.修改ngnix配置文件

测试配置文件/usr/local/nginx/sbin/./nginx -t

# cd /usr/local/nginx/conf

# vi nginx.conf

#使用的用户和组

user  root root;

#制定工作衍生进程数(一般为CPU总核数两倍)

worker_processes  8;

#制定文件描述符数量

worker_rlimit_nofile 51200;

#指定错误日志存放路径

error_log  logs/error.log

#指定pid存放路径

pid        logs/nginx.pid;

event

{

#使用网络I/O模型

use epoll;

#允许的连接数

worker_connections  65535;

}

http {

#访问日志存放路径

access_log logs/access.log;

#开启gzip压缩

gzip             on;

gzip_min_length  1000;

gzip_proxied     expired no-cache no-store private auth;

gzip_types       text/plain text/css text/xml text/javascript application/x-javascript application/xml application/rss+xml application/xhtml+xml application/atom_xml;

gzip_disable "MSIE [1-6].(?!.*SV1)";

#限定PHP-CGI的连接、发送和读取的时间为300s

fastcgi_connect_timeout 300s;

fastcgi_send_timeout 300s;

fastcgi_read_timeout 300s;

#虚拟主机配置

server {

#监听端口

listen 8087;

#主机名称

server_name 127.0.0.1;

#google提供的DNS服务

resolver 8.8.8.8

location / {

#nginx作为HTTP代理服务器

proxy_pass http://$http_host$request_uri;

proxy_set_header Accept-Encoding '';

proxy_redirect          off;

}

}

}

三. 排错优化

1)修改环境变量

vi ~/.bashrc

PATH=$PATH:/usr/local/nginx/sbin:/usr/local/varnish/sbin:/usr/local/varnish/bin

export PATH

2)nginx启动与关闭

参数:

-v:查看版本

-V:查看配置模块

-t:查看配置文件是否正确

-c:制定其他配置文件

pkill -9 nginx

3)varnish启动与关闭

参数:

-u:以什么用户运行

-g:以什么组运行

-f:varnish配置文件

-a:绑定IP和端口

-s:varnish缓存位置和大小

-w:最小,最大线程和超时时间

-T:varnish管理端口,主要用来清除缓存

pkill varnishd

varnish服务动态加载配置文件

#varnishadm -T 127.0.0.1:2000

vcl.load vcl-name_vcl "配置文件路径" # vcl-name 这里可以任意取名

vcl.use vcl-name

vcl.show vcl-name #显示vcl-name配置文件内容

# varnishncsa -w /usr/local/varnish/logs/varnish.log

将输出日志写入到制定日志文件。

4)修改windows客户端

C:WindowsSystem32driversetchosts

192.168.1.202 www.baidu.com

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。