# vim /etc/varnish/varnish.params
VARNISH_LISTEN_PORT=80 --> 修改为80端口
# vim /etc/varnish/default.vcl
backend html {
.host = "10.1.249.125";
.port = "80";
}
backend app {
.host = "10.1.252.53";
.port = "80";
}
sub vcl_recv {
if (req.url ~ "(?i)\.php$") { --> (?i) 不区分字符大小写
set req.backend_hint = app;
} else {
set req.backend_hint = html;
}
}
# varnish_reload_vcl default.vcl --> 修改了vcl文件,需要重新编译
sub vcl_recv {
if (req.url ~ "(?i)\.php") { --> 将原来.php$ 的 $ 去掉即可
set req.backend_hint = app;
} else {
set req.backend_hint = html;
}
}
# varnish_reload_vcl default.vcl
# systemctl restart varnish
import directors; --> 放在vcl 4.0; 后面
backend default {
.host = "10.1.249.125";
.port = "80";
}
backend server2 {
.host = "10.1.252.53";
.port = "80";
}
sub vcl_init {
new webapp = directors.round_robin();
webapp.add_backend(default);
webapp.add_backend(server2);
}
sub vcl_recv {
set req.backend_hint = webapp.backend(); //顺序很重要,顺序很重要,顺序很重要,放在if 语句的前面
if (req.url ~ "^/test") { -->定义在该路径下的内容不缓存,否则无法看出负载均衡的效果
return(pass); -->第一次选中某台服务器后,后续可能会重缓存中直接返回结果
}
}
probe check { --> 需要写在两个后端服务器之前,顺序很重要,顺序很重要,顺序很重要
.url = "/test/index.html";
.window = 5;
.threshold = 4;
.interval = 2s;
.timeout = 1s;
}
backend default {
.host = "10.1.249.125";
.port = "80";
.probe = check;
}
backend server2 {
.host = "10.1.252.53";
.port = "80";
.probe = check;
}
原创文章,作者:sixijie,如若转载,请注明出处:http://www.178linux.com/59043