反向代理:缓存
http/1.1
RFC
http/1.1《-----http/1.0
cache
http协议:
request:
method url version
HEADERS
BODY
response:
version status reason
HEADERS
BODY
首部:
通用
Connection:close|keep-alive
Date:日期时间
Host:请求的主机
Pragma:no-cahce
Via:请求或响应在客户端和服务器之间传递时所经过的代理
Transfer-Encoding:消息主体的传输编码方式,chunked表示采用块编码的方式
请求
If-Modified-Since
If-None-Match
Referer
User-Agent
Host:请求的主机
Accept-Encoding:接受的编码方式
Accept-Language:接受的自然语言
Accep-Charset:接受字符集
Authorization:服务端发送www-authenticate时,客户端通过此首部提供认证信息
响应
ETag:内容的标签
Location:重定向后的新位置
Server:服务器软件信息
WWW-Authenticate:要求对客户端进行认证
实体
Content-Encoding
Content-Language
Conten-Lenth
Content-Type:内容的MIME格式
Expires
Last-Modified
web object
browser:缓存机制
本地:
公共缓存(public)
私有缓存(private)
expire(过期时间):2016-05-21 14:55:31
cache-control:max-age=600
etag(标识符):一个时间随机生成数
last-mofified:
if-modified-since:
if-none-match:
vary:
age:
index.html:14:55:59
14:55:59,0000011:
条件式判断机制
CDN(content delivery network):内容分发网络,一组缓存服务器组成的网络(集群)
www.maoshou.com in CNAME cache1.cdn.com
www.maoshou.com in CNAME cache4.cdn.com
bind,view
bind-dlz+MySQL
pgsql,oracle,db4
Varnish:
web缓存、代理服务器;
squid:varnish
httpd:nginx
nginx+varnish
nginx+squid
varnish配置要自己去编写配置文件:采用vcl编写工具
状态引擎:子例程,函数,可以使用return()函数返回状态给varnish进程
vcl_recv
vcl_pass
vcl_hash
vcl_hit
vcl_deliver
vcl_miss
vcl_fetch
vcl_deliver
安装配置:
源始服务器:node2
varnish节点:node1(两块网卡)
node2:
yum install php php-mysql mysql-server
ifconfig eht0 192.168.20.12/24 up
vim /var/www/html/index.html
node2.magedu.com
service httpd restart
node1:
ifconfig eth0 192.168.100.7/24 up
ifconfig eth1 192.168.20.1/24 up
yum install varnish
rpm -ql varnish
vim /etc/sysconfig/varnish
VARNISH_LISTEN=80
VARNISH_STORAGE="malloc,100M"
service varnish start
cd /etc/varnish
vim test.vcl
backend websrv1 {
.host = "192.168.20.12";
.port = "80";
}
让自己写的配置文件生效:
vim /etc/sysconfig/varnish
VARNISH_VCL_CONF=/etc/varnish/test.vcl
或者varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
help
ping
status
stats
vcl.list
vcl.show boot
vcl.load test1 /etc/varnish/test.vcl
vcl.list
vcl.use test1
vcl.show test1
http://172.16.100.7
curl -I http://192.168.100.7
vim test.vcl
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}
vcl.load test2 /etc/varnish/test.vcl
vcl.use test2
vcl.show test2
curl -I http://172.16.100.7
crul -I http://172.16.100.7/index.html
node2:
cd /var/www/html
vim test.html
test on node2.magedu.com
curl -I http://172.16.100.7/test.html
node1:
vim test.vcl
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from "server.ip;
} else {
set resp.http.X-Cache = "MISS";
}
}
vcl.load test3 /etc/varnish/test.vcl
vcl.use test3
vcl.show test3
vcl -I http://172.16.100.7/test.html
vim test.vcl
sub vcl_recv {
if (req.url ~ "test.html") {
return(pass);
}
return(lookup);
}
vcl.load test4 /etc/varnish/test.vcl
vcl.use test4
curl -I http://172.16.100.7/index.html
curl -I http://172.16.100.7/test.html
vim test.vcl
sub vcl_fetch {
if (req.url ~ ".(jpg|jpeg|gif|png)$") {
set beresp.ttl = 7200s;
}
if (req.url ~ ".(html|css|js)$")
set beresp.ttl = 1200s;
}
}
vcl.load test5 /etc/varnish/test.vcl
vcl.use test5
curl -I http://172.16.100.7/index.html
删除缓存:
vim test.vcl
acl purgers {
"127.0.0.1";
"172.16.0.0"/16;
}
sub vcl_recv {
if (req.request == "PURGE") {
if (!client.ip ~ purgers) {
error 405 "Method not allowed";
}
}
if (req.url ~ "test.html") {
return(pass);
}
return(lookup);
}
sub vcl_hit {
if (req.request == "PURGE") {
purge;
error 200 "purged ok";
}
}
sub vcl_miss {
if (req.request == "PURGE") {
purge;
error 404 "not in cache";
}
}
sub vcl_pass {
if (req.request == "PURGE") {
error 502 "purged on a passed object";
}
}
vcl.load test6 /etc/varnish/test.vcl
vcl.use test6
curl -I http://172.16.100.7/index.html
curl -X PURGE http://172.16.100.7/index.html
多台后端主机的健康状况检查:(node2和node3)
ifconfig eth0 192.168.20.13/24 up
node1:
vim test.vcl
backend web1 {
.host = "192.168.20.12";
.port = "80";
}
director wesrvs random {
{
.backend = web1;
.weight = 2;
}
{
.backend = {
.host = "192.168.20.13";
.port = "80";
}
weight = 1;
}
}
或者:
backend web1 {
.host = "192.168.20.12";
.port = "80";
.probe = { (让该节点离线)
.url = "/index.html";
.window = 5;
.threshold = 2;
.interval = 3s;
}
}
backend web2 {
.host = "192.168.20.13";
.port = "80";
}
director websrvs random {
{
.backend = web1;
.weight = 2;
}
{
.backend = web2;
.weight = 1;
}
}
sub vcl_recv {
set req.backend = websrvs;
if (req.request == "PURGE") {
if (!client.ip ~ purgers) {
error 405 "Method not allowed";
}
}
if (req.url ~ "test.html") {
return(pass);
}
return(lookup);
}
vcl.load test7 test.vcl
vcl.use test7
curl http://172.16.100.7/index.html
请求不同内容时,转发至不同主机:
vim test.vcl
把director那一栏全部注释
sub vcl_recv {
if (req.url ~ ".(html|css|js)$") {
set req.backend = web1;
} else {
set req.backend = web2;
}
if (req.request == "PURGE") {
if (!client.ip ~ purgers) {
error 405 "Method not allowed";
}
}
if (req.url ~ "test.html") {
return(pass);
}
return(lookup);
}
vcl.load test8 test.vcl
vcl.use test8