1、代理服务器Squid 1.1Squid服务器 反向代理工作机制:代替公网用户访问组织内部的web服务器 部署基于地区的CDN服务器 软件包:squid 系统服务:squid 主程序:/usr/sbin/squid 主配置文件:/etc/squid/squid.conf 默认监听端口:TCP 3128 默认访问日志:/var/log/squid/access.log proxy : 代理 1.2部署步骤 后台真正的服务器(web1) 1)yum -y install httpd systemctl start httpd systemctl enable httpd 2)在/var/www/http写页面 部署squid代理服务器(proxy) 1)yum -y install squid 2)vim /etc/squid/squid.conf http_port 80 vhost #设置访问的web说明是代理服务器 visible_hostname svr5.tarena.com #设置主机名,默认没有该语句 visible(可见的) cache_peer 192.168.2.100 parent 80 0 originserver #定义后端真实服务器信息 origin起源 cache_dir ufs /var/spool/squid 200 16 256
#硬盘缓存,缓存容量为200M,自动创建16个一级子目录和256个二级子目录 http_access allow all #允许本机所有主机使用代理服务器 3)启动squid服务 systemctl start squid systemctl enable squid 4)客户端测试 crul http://192.168.4.5(代理服务器) #显示后台真正服务器的页面 [root@Client ~]# curl -I http://192.168.4.5 #查看头部信息 HTTP/1.1 200 OK Last-Modified: Wed, 03 Jan 2018 02:55:21 GMT Accept-Ranges: bytes Content-Length: 14 Content-Type: text/html; charset=UTF-8 Date: Wed, 03 Jan 2018 03:54:50 GMT Server: Apache/2.4.6 (Red Hat Enterprise Linux) #服务端使用的服务 ETag: "e-561d6589eaaf0" Age: 0 X-Cache: HIT from Proxy #hit 表明缓存给的页面 miss表示不是缓存 X-Cache-Lookup: HIT from Proxy:80 Via: 1.1 Proxy (squid/3.3.8) Connection: keep-alive

2、Varnish 缓存在内存或者硬盘(squid在硬盘) 2.1构建Web服务器 使用yum安装web软件包(httpd) 启用httpd服务,并设为开机自动运行 2.2部署Varnish缓存服务器 ⑴编译安装软件 [root@svr5 ~]# yum -y install gcc readline-devel pcre-devel gcc-c++ //安装软件依赖包 [root@svr5 ~]# useradd -s /sbin/nologin varnish //创建账户 [root@svr5 ~]# tar -xzf varnish-3.0.6.tar.gz [root@svr5 ~]# cd varnish-3.0.6 [root@svr5 varnish-3.0.6]# ./configure --prefix=/usr/local/varnish [root@svr5 varnish-3.0.6]# make && make install ⑵复制启动脚本及配置文件 [root@svr5 varnish-3.0.6]# cp redhat/varnish.initrc /etc/init.d/varnish [root@svr5 varnish-3.0.6]# cp redhat/varnish.sysconfig /etc/sysconfig/varnish [root@svr5 varnish-3.0.6]# ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/ [root@svr5 varnish-3.0.6]# ln -s /usr/local/varnish/bin/* /usr/bin/ ⑶修改Varnish文件 vim /etc/sysconfig/varnish #前端配置 vim /etc/varnish/default.vcl #后端配置 [root@svr5 ~]# vim /etc/sysconfig/varnish 66行:VARNISH_LISTEN_PORT=80 #默认端口 89行:VARNISH_STORAGE_SIZE=64M #定义缓存大小 92行:VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}" #基于内存方式缓存 (4)修改代理配置文件 [root@svr5 ~]# mkdir /etc/varnish [root@svr5 ~]# cp /usr/local/varnish/etc/varnish/default.vcl /etc/varnish/ [root@svr5 ~]# uuidgen > /etc/varnish/secret [root@svr5 ~]# vim /etc/varnish/default.vcl backend default { .host = "192.168.2.100"; .port = "80"; } 程序【硬盘】 program 进程【内存】 process 线程【进程下多个线程】thread

2.3客户端测试 service varnish start curl http://192.168.4.5 /etc/init.d/varnish start 2.4其他操作 ⑴查看varnish日志 [root@svr5 ~]# varnishlog #varnish日志 ping自己的后台 [root@svr5 ~]# /usr/local/varnish/bin/varnishlog #没有做链接文件的操作 [root@svr5 ~]# varnishncsa #访问日志 客户端访问才有反应 [root@server01 ~]# /usr/local/varnish/bin/varnishcsa [root@server01 ~]# /usr/local/varnish/bin/varnishadm stop #停止服务 [root@server01 ~]# /usr/local/varnish/bin/varnishadm start #启动服务 [root@server01 ~]# /usr/local/varnish/bin/varnishadm status #查看状态 [root@server01 ~]# /usr/local/varnish/bin/varnishadm ban.url / #清空缓存 ⑵更新缓存数据,在后台web服务器更新页面内容后,用户访问代理服务器看到的还是之前的数据,说明缓存中的数据过期了需要更新(默认也会自动更新,但非实时更新) [root@svr5 ~]# varnishadm –S /etc/varnish/secret –T 127.0.0.1:6082 ban.url 页面文件名(/根) #清空缓存数据,支持正则表达式(非交互) 缓存过期: 1)自己更新 2)手动更新(在/etc/sysconfig/varnish查看端口以及安全文件)(交互) varnishadm –S /etc/varnish/secret –T 127.0.0.1:6082 #进入 backend.list #查看后端的信息 ban.url .* #所有更新 ⑶varnish状态 varnishstat Client connections accepted #表示客户端成功发送连接总数量 Client requests received #客户端发送http请求的总数 Cache hits #命中缓存的次数 Cache misses #缓存非命中的个数

netstat -anptu | grep 80