主機爲1.1.1.20 我用一臺主機好測試,web服務器也爲這個.端口爲90
修改主機名 hostname juan.lmz.com sed -i 's@\(HOSTNAME=.*\)@\1juan.lmz.com@' /etc/sysconfig/network
hostname juan.lmz.com
修改varish的配置文件.主要修改用戶LISTEN的端口和存儲的方式 [root@juan html]# cat /etc/sysconfig/varnish |grep -En 'VARNISH_LISTEN_PORT=80|VARNISH_STOTAGE' 66: VARNISH_LISTEN_PORT=80 86:VARNISH_STOTAGE="malloc,128M"
配置VCL的配置語言 vi /etc/varnish/default.vcl backend default { .host = "1.1.1.20"; .port = "90"; } sub vcl_recv { if (req.url ~ "test.html$") { return(pass); } } sub vcl_fetch { if (req.request == "GET" && req.url ~ ".html$") { set beresp.ttl = 10s; } } sub vcl_deliver { if (obj.hits > 0){ set resp.http.X-Cache = "Hit Via" + " " + server.hostname; } else { set resp.http.X-Cache = "Miss from" + " " + server.hostname; } } backend default { .host = "1.1.1.20"; 後端服務器地址 .port = "90"; 後端服務器端口 } sub vcl_recv { if (req.url ~ "test.html$") { 定義匹配的URL return(pass); 就是不經過varnish緩存 } } sub vcl_fetch { if (req.request == "GET" && req.url ~ ".html$") { set beresp.ttl = 10s;緩存10s } } sub vcl_deliver { 定義響應報文,這裏定義的是主機頭 if (obj.hits > 0){ set resp.http.X-Cache = "Hit Via" + " " + server.hostname; } else { set resp.http.X-Cache = "Miss from" + " " + server.hostname; } }
這個是頁面文件的內容 [root@juan html]# pwd /var/www/html [root@juan html]# cat index.html juan lmz [root@juan html]# cat test.html juan
分別訪問1.1.1.20/index.html 和1.1.1.20/test.index.html