Varnish+Nginx搭建緩存服務器

jopen 10年前發布 | 34K 次閱讀 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:\Windows\System32\drivers\etc\hosts 192.168.1.202 www.baidu.com</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!