base安装(操作步骤如下):
ifconfig
hostname server1
vi /etc/sysconfig/network
vi /etc/yum.repos.d/rhel-source.repo ##配置yum源
yum repolist ##yum的更新
yum install -y vim lftp
yum install -y openssh-clients
cd /etc/udev/
ls
cd rules.d/
rm -fr 70-persistent-net.rules
ip addr
cd/etc/sysconfig/network-scripts/
vim ifcfg-eth0
vim /etc/hosts
cd /etc/ssh/
rm -fr ssh_host_* ##删除ssh所给的钥匙(如果ssh连接出现问题,就在这删除再次连接)
iptables -L
cd /etc/sysconfig/
rm -fr iptables
ls
pwd
chkconfig iptables off ##关闭火墙
vim /etc/sysconfig/selinux ##在文件中将selinux状态改为disabled
vm1(varnish轮询主机);
yum install -y *
(安装varnish.x86_64 0:3.0.5-1.el6 varnish-libs.x86_64 0:3.0.5-1.el6这两个安装包下载安装varnish )
cd /etc/varnish/
vim /etc/sysconfig/varnish (设置varnish默认端口是80VARNISH_LISTEN_PORT=80)
/etc/init.d/varnish reload
vim /etc/varnish/default.vcl ##配置varnish
/etc/init.d/varnish start
/etc/init.d/varnish reload
-------------------------------------------------------------------------------------------
###varnish的配置
[root@server1 varnish]# cat default.vcl
###定义多个不同域名站点的后端服务器
backend web1 {
.host = "172.25.78.2"; ###配置一个后端服务器
.port = "80";
}
backend web2 {
.host = "172.25.78.3";
director lb round-robin { #把多个后端聚合为一个组,并检测后端健康状况
{.backend = web1;}
{.backend = web2;}
<a href="https://s1.51cto.com/wyfs02/M00/9C/AD/wKioL1l0f26C19AuAAC4oB_cxt4199.png" target="_blank"></a>
#当访问 www.westos.org 域名时从 web1 上取数据,访问 bbs.westos.org 域名时到 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 = web1;
} 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);
###测试缓存命中
[root@foundation78 ~]# curl -I www.westos.org
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 19 Jul 2017 14:21:56 GMT
ETag: "9f614-20-554ac5ab83cc8"
Content-Type: text/html; charset=UTF-8
Content-Length: 32
Accept-Ranges: bytes
Date: Thu, 20 Jul 2017 03:22:27 GMT
X-Varnish: 1895199082
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache #未命中
Date: Thu, 20 Jul 2017 03:22:29 GMT
X-Varnish: 1895199083 1895199082
Age: 2
X-Cache: HIT from westos cache ##命中
----------------------------------------------------------------------------------------------------------------------
### 通过 varnishadm 手动清除缓存
# varnishadm ban.url .*$ #清除所有
# varnishadm ban.url /index.html #清除 index.html 页面缓存
# varnishadm ban.url /admin/$ #清除 admin 目录缓存
在vm2上(充当web1):
vim /etc/httpd/conf/httpd.conf
NameVirtualHost *:80(打开此端口)
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName server2
</VirtualHost>
DocumentRoot /www/bbs
ServerName bbs.westos.org
DocumentRoot /www/westos
ServerName www.westos.org
yum install -y httpd
/etc/init.d/httpd start
cd /var/www/html/
ls
vim index.html
vim /etc/httpd/conf/httpd.conf
mkdir /www/bbs -p
mkdir /www/westos
cd /www/bbs/
cd /www/westos/
/etc/init.d/httpd restart
vim /etc/hosts (给www.westos.org和bbs.westos.org作解析)
vm3(充当web2):
yum install -y httpd
/etc/init.d/httpd start
cd /var/www/html/
vim index.html
在物理机测试:
vim /etc/hosts (作解析)
curl 172.25.78.1
curl bbs.westos.org
curl www.westos.org
curl www.westos.org/index.html
------------------------------------------------------------------------------------------------------------------
###varnish cdn 推送平台####
CDN推送
1.vim /etc/httpd/conf/httpd.conf
因为做varnish时候把监听端口改成了80,所以做cdn推送时候要更改http的监听端口为8080
2.yum install unzip.x86_64 -y
3.unzip bansys.zip -d /var/www/html/
解压bansys到html目录下,可以直接网页推送(cd /var/www/html/bansys/ mv * .. rm -fr bansys/)
4.yum install -y php
因为cdn里面的文件是使用php编写的,需要下载php编译器(需要安装 php 支持)
5./etc/init.d/varnish start
/etc/init.d/httpd start
6.vim /var/www/html/config.php(配置文件更改如下)
*****************************************
*<?php
* $var_group1 = array(
* 'host' =>array('172.25.78.1'),
* 'port' => '80',
*);
*
*//varnish 群组定义
*//对主机列表进行绑定
*$VAR_CLUSTER = array(
* 'www.westos.org' =>$var_group1,
*//varnish 版本//2.x 和 3.x 推送命令不一样
*$VAR_VERSION = "3";
*?>
******************************************
#bansys 有两种工作模式,分别是:telnet 和 http 模式。
#telnet 模式需要关闭 varnish 服务管理端口的验证,注释掉/etc/sysconfig/varnish 文件中的 “ -S ${VARNISH_SECRET_FILE}”这行,重启 varnish 服务即可。
#如果是 http 模式需要对 varnish 做以下设置:
# vim /etc/varnish/default.vcl
acl westos {
#设置访问控制
"127.0.0.1";
"192.168.0.0"/24;
if (req.request == "BAN"){
if (!client.ip ~ westos) {
error 405 "Notallowed.";
}
ban("req.url ~ " +req.url);
error 200 "ban added";
}
# service varnish reload
本文转自 yab109 51CTO博客,原文链接:http://blog.51cto.com/12768057/1949519,如需转载请自行联系原作者