- 安装varnish调度服务器
1.安装varnish
cd ~
yum install * -y
2.修改端口
vi /etc/sysconfig/varnish
66行改为80
3.关闭server1
把内存1024改为2048
4. 因为vi /etc/sysconfig/varnish
第8行NFILES=131072
第12行MEMLOCK=82000
第15行NPROCS=“unlimited”
98860<131072所以要加内存
5.修改/etc/varnish/default.vcl第8行
.host = “172.25.16.2”;
重启服务/etc/init.d/varnish restart
6.server2安装httpd
yum install -y httpd
7.写默认发布页/var/www/html/index.html
vim /var/www/html/index.html
server2
8.server2开启服务
/etc/init.d/httpd start
真机浏览器连接172.25.16.1
出现172.25.16.2里写的发布页
- 手动清除cdn缓存
server1写配置文件
vim /etc/varnish/default.vcl
sub vcl_deliver {
if (obj.hits > 0 ) {
set resp.http.X-Cache = "HIT from westos cache";
}
else {
set resp.http.X-Cache ="MISS from westos cache";
}
return(deliver);
}
清除所有缓存
varnishadm ban.url .*$
真机
curl -I 172.25.16.1
清除后第一次连接为miss此后都为HIT
- 解析的方式配置多个后端服务器
1.server1写配置文件,重启服务
vim /etc/varnish/default.vcl
backend web1 {
.host = "172.25.16.2";
.port = "80";
}
backend web2 {
.host="172.25.16.3";
.port="80";
}
sub vcl_recv{
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = web1;
}elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {
error 404"westos cache";
}
}
/etc/init.d/varnish restart
2.server3安装httpd,写配置文件并且开服务
yum install httpd -y
vim /var/www/html/index.html
server3
/etc/init.d/httpd start
3.写真机的解析
172.25.16.1 server1 www.westos.org bbs.westos.org
测试
- 负载均衡
让两台服务器来回接收访问
server1修改
vim /etc/varnish/default.vcl
backend web1 {
.host = "172.25.16.2";
.port = "80";
}
backend web2 {
.host="172.25.16.3";
.port="80";
}
director lb round-robin{
{ .backend = web1; }
{ .backend = web2; }
}
sub vcl_recv{
if (req.http.host ~ "^(www.)?westos.org"){
set req.http.host = "www.westos.org";
set req.backend = lb;
return (pass);
}elsif (req.http.host ~ "^bbs.westos.org"){
set req.backend = web2;
}else {
error 404 "westos cache";
}
}
/etc/init.d/varnish restart
真机测试
- 一个后端服务器多域名解析
一台主机多个解析(虚拟主机):
打开虚拟主机的端口
在server3上配置多个解析地址
先打开apache配置文件
vim /etc/httpd/conf/httpd.conf
搜索NameVirtualHost
找到之后将80端口号这条命令的注释放开(在990行)
在最后编辑:
<VirtualHost *:80>
DocumentRoot /www
ServerName www.westos.org
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /bbs
ServerName bbs.westos.org
</VirtualHost>
但是/www 和/bbs这两个目录不存在,所以要先建立后再写发布页并重启
mkdir /www /bbs
vim /www/index.html
www.server3
vim /bbs/index.html
bbs.server3
/etc/init.d/httpd restart
真机:
vim /etc/hosts
172.25.16.3 server3 www.westos.org bbs.westos.org
测试:
curl www.westos.org
curl bbs.westos.org
- cdn推送一键清除缓存
server1
安装httpd和php
yum install -y httpd php
修改端口
vim /etc/httpd/conf/httpd.conf
136行改为8080
/etc/init.d/httpd start
查看端口
netstat -antlpe
下载bansys:jxfu
安装解压程序
yum install unzip -y
把解压后的文件放入apache发布目录
cd ~
unzip bansys.zip
cd bansys
mv * /var/www/html/
删除原配置文件 config.php内容,重新写内容
vim /var/www/html/config.php
<?php
$var_group1 = array(
'host' => array('172.25.16.1'),
'port' => '8080',
);
//varnish群组定义
//对主机列表进行绑定
$VAR_CLUSTER = array(
'www.westos.com' => $var_group1,
);
//varnish版本
//2.x和3.x推送命令不一样
$VAR_VERSION = "3";
?>
开服务
/etc/init.d/httpd restart
修改varnish配置文件
vi /etc/varnish/default.vcl
写
acl westos{
"127.0.0.1"; ##允许本机访问
"172.25.16.0"/24; ##允许172.25.64网段的所有主机访问
}
sub vcl_recv{
if(req.request == "BAN"){
if(!client.ip ~ westos){
error 405 "Not allowed.";
}
ban("req.url ~" + req.url);
error 200 "ban added";
}
}
backend web1 {
.host = "172.25.16.2";
.port = "80";
}
backend web2 {
.host = "172.25.16.3";
.port = "80";
}
director lb round-robin {
{ .backend = web1;}
{ .backend = web2;}
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb;
#return (pass);
}elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {
error 404 "westos cache";
}
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from westos.cache";
}
else {
set resp.http.X-Cache = "MISS from westos cache";
}
return (deliver);
}
真机:
vim /etc/hosts
172.25.16.1 server1 www.westos.org bbs.westos.org
server1:
vim /etc/varnish/default.vcl
测试
curl -I www.westos.org
连接两次后为HIT
真机浏览器连接
172.25.16.1:8080
写下
提交
真机连接此时缓存为miss