操作环境:
三台服务器:
一台 网站服务器(http)
两块网卡 1 仅主机 2 自定义VMnet2
一台 Squid代理
两块网卡 1 仅主机 2 自定义VMnet2
一台 客户机
两块网卡 1 仅主机 2 自定义VMnet2
---------------
网站服务器:
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0 //首先先关闭“天敌”
然后安装http
yum install httpd -y
然后添加测试页面 看看服务是否可以正常启动:
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# echo "<h1>aaaaaa</h1>" > index.html
[root@localhost html]# ls
index.html
[root@localhost html]# vi index.html
[root@localhost html]# systemctl start httpd
[root@localhost html]# dd if=/dev/zero of=test1.tgz bs=1M count=11
记录了11+0 的读入
记录了11+0 的写出
11534336字节(12 MB)已复制,0.0323409 秒,357 MB/秒
[root@localhost html]# dd if=/dev/zero of=test2.tgz bs=1M count=2
记录了2+0 的读入
记录了2+0 的写出
2097152字节(2.1 MB)已复制,0.00621181 秒,338 MB/秒
[root@localhost html]# ll -h
总用量 14M
-rw-r--r--. 1 root root 16 1月 22 09:11 index.html
-rw-r--r--. 1 root root 11M 1月 22 09:44 test1.tgz
-rw-r--r--. 1 root root 2.0M 1月 22 09:44 test2.tgz
[root@localhost html]# rm -rf index.html
-----------------------网站服务器完成
Squid代理 :
安装squid代理服务器
yum install gcc gcc-c++ make -y
yum install perl-devel -y
tar xf squid-3.5.27.tar.gz -C /opt/
cd /opt/squid-3.5.27/
./configure \
--prefix=/usr/local/squid \
--sysconfdir=/etc \
--enable-arp-acl \
--enable-linux-netfilter \
--enable-linux-tproxy \
--enable-async-io=100 \
--enable-err-language="Simplify_Chinese" \
--enable-underscore \
--enable-poll \
--enable-gnuregex //安装环境 配置文件
注:
./configure \ #配置
--prefix=/usr/local/squid \ #指定安装路径
--sysconfdir=/etc \ #配置文件存储目录
--enable-arp-acl \ #可在ACL中设置通过MAC地址进行管理,防止IP欺骗
--enable-linux-netfilter \ #使用内核过滤,目的是对透明模式提供支持
--enable-linux-tproxy \ #允许使用透明模式
--enable-async-io=100 \ #异步I/O,用来提升存储性能。
--enable-err-language="Simplify_Chinese" \ #
--enable-underscore \ #允许URL中有下划线
--enable-poll \ #使用Poll()模式,提升性能
--enable-gnuregex #使用GNU正则表达式
make && make install //编译安装
ln -s /usr/local/squid/sbin/* /usr/local/sbin
useradd -M -s /sbin/nologin squid
chown -R squid:squid /usr/local/squid/var/
vi /etc/squid.conf
http_access allow all
http_port 3128 //在下面新增
visible_hostname 192.168.80.181 #确定公共主机名
cache_mem 64 MB
cache_swap_low 80
cache_swap_high 97
cache_dir ufs /usr/local/squid/var/cache/squid 512 16 256 //配置硬盘缓存,打开#.缓存目录512M,其中一级目录16个,二级256个
cache_effective_user squid #用来设置初始化、运行时缓存的账号,否则启动不成功
cache_effective_group squid #//默认为指定账号的基本组
squid -k parse //检查配置文件
squid –k rec //重新加载配置文件
squid -zX //初始化缓存目录
--------------制作squid系统服务脚本---------
为了使Squid服务的启动、停止、重载等操作更加方便,可以编写Squid服务脚本,并使用chkconfig和service工具来进行管理。
vi /etc/init.d/squid
#!/bin/bash
#chkconfig: 35 90 25
#config: /etc/squid.conf
#pidfile: /usr/local/squid/var/run/squid.pid
#Description: Squid - Internet Object Cache
PID="/usr/local/squid/var/run/squid.pid"
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"
case "$1" in
start)
netstat -utpln | grep squid &>/dev/null
if [ $? -eq 0 ]
then
echo "Squid is running"
else
$CMD
fi
;;
stop)
$CMD -k kill &>/dev/null
rm -rf $PID &>/dev/null
;;
status)
[ -f $PID ] &>/dev/null
if [ $? -eq 0 ]
then
netstat -utpln | grep squid
else
echo "Squid is not running"
fi
;;
restart)
$0 stop &>/dev/null
echo "正在关闭Squid..."
$0 start &>/dev/null
echo "正在启动Squid..."
;;
reload)
$CMD -k reconfigure
;;
check)
$CMD -k parse
;;
*)
echo "用法:{start | stop | restart | reload | check | status}"
esac
chmod +x /etc/init.d/squid
chkconfig --add squid
chkconfig squid on
service firewalld stop
setenforce 0
service squid start
netstat -anpt | grep 3128
重启squid代理服务
测试,在windows浏览器中设置代理服务地址,输入192.168.80.181/test1.tgz。出现一下:
查看Squid访问日志的新增记录
tail /usr/local/squid/var/logs/access.log //可以看到客户机C访问Web服务器的记录
查看Web访问日志的新增记录
tail /var/log/httpd/access_log //可以看到来自Squid服务器的访问记录,Squid服务器代替客户机C访问Web服务器
当客户机再次访问同一页面时,Squid访问日志会增加新的记录,而Web访问日志的记录不会变化(除非页面变更或强制刷新等操作)。这说明当客户机访问同一静态页面时,实际上是由代理服务器通过缓存提供的.