Varnish,Nginx搭建緩存服務器

一. 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.一、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:\Windows\System32\drivers\etc\hosts
192.168.1.202 www.baidu.com
相關文章
相關標籤/搜索