緩存代理概述
web代理的工做機制:
緩存網頁對象,減小重複請求

代理的基本類型
傳統代理:適用於Internet,需明確指定服務端
透明代理:客戶機不須要指定代理服務器的地址和端口,是經過默認路由,防火牆將web重定向給代理
使用代理的好處
提升web訪問速度
隱藏客戶機的真實IP地址
一,傳統代理
實驗環境
squid服務器 192.168.13.179
web服務器 192.168.13.151
client測試機192.168.13.135
1,在squid服務器上安裝squid代理服務器
[root@squid ~]# mkdir /abc
[root@squid ~]# mount.cifs //192.168.100.3/LNMP-C7 /abc/ ##掛載
[root@squid ~]# cd /abc/
[root@squid abc]# tar zxvf squid-3.4.6.tar.gz -C /opt ##解壓
[root@squid abc]# yum install gcc gcc-c++ make -y ##安裝環境組件
[root@squid abc]# cd /opt/squid-3.4.6
[root@squid squid-3.4.6]# ./configure \
--prefix=/usr/local/squid \ ##安裝路徑
--sysconfdir=/etc \ ##配置文件目錄
--enable-arp-acl \ ##支持acl訪問控制列表
--enable-linux-netfilter \ ##支持網絡篩選
--enable-linux-tproxy \ ##支持透明
--enable-async-io=100 \ ##io優化
--enable-err-language="Simplify_Chinese" \ ##報錯顯示簡體中文
--enable-underscore \
--enable-poll \
--enable-gnuregex ##支持正則表達
[root@squid squid-3.4.6]# make && make install ##編譯安裝
[root@squid squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/ ##便於系統識別
[root@squid squid-3.4.6]# useradd -M -s /sbin/nologin squid ##建立系統用戶
[root@squid squid-3.4.6]# chown -R squid.squid /usr/local/squid/var/ ##給目錄全部文件屬主屬組權限
2,修改squid配置文件,並優化啓動項
[root@squid squid-3.4.6]# vim /etc/squid.conf ##修改squid配置文件
# And finally deny all other access to this proxy
http_access allow all ##添加此項
#http_access deny all ##註釋,容許終端訪問
# Squid normally listens to port 3128
http_port 3128
cache_effective_user squid ##指定用戶squid
cache_effective_group squid ##指定組
[root@squid squid-3.4.6]# squid -k parse ##檢查配置文件語法
[root@squid squid-3.4.6]# squid -z ##初始化緩存目錄
[root@squid squid-3.4.6]# squid ##開啓服務
[root@squid squid-3.4.6]# netstat -ntap | grep 3128 ##查看squid端口
[root@squid squid-3.4.6]# cd /etc/init.d/
[root@squid init.d]# vim squid ##編輯service啓動squid的腳本
#!/bin/bash
#chkconfig: 2345 90 25
PID="/usr/local/squid/var/run/squid.pid" ##PID文件進程號
CONF="/etc/squid.conf" ##主配置文件
CMD="/usr/local/squid/sbin/squid" ##啓動命令
case "$1" in
start)
netstat -ntap | grep squid &> /dev/null
if [ $? -eq 0 ]
then
echo "squid is running"
else
echo "正在啓動 squid...."
$CMD
fi
;;
stop)
$CMD -k kill &> /dev/null ##關閉squid
rm -rf $PID &> /dev/null ##刪除PID文件
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ]
then
netstat -ntap | 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 "用法:$0{start|stop|reload|status|check|restart}"
;;
esac
[root@squid init.d]# chmod +x squid ##給執行權限
[root@squid init.d]# chkconfig --add squid ##添加到service管理中
[root@squid init.d]# chkconfig --level 35 squid on ##開機自啓
3,設置傳統代理配置
[root@squid init.d]# vim /etc/squid.conf ##修改主配置文件
# Squid normally listens to port 3128
http_port 3128
cache_mem 64 MB ##內存空間大小
reply_body_max_size 10 MB ##容許下載最大文件大小
maximum_object_size 4096 KB ##容許保存緩存空間最大對象大小
[root@squid init.d]# service squid restart
[root@squid init.d]# iptables -L ##查看錶內容
[root@squid init.d]# iptables -F ##清空表緩存
[root@squid init.d]# setenforce 0
[root@squid init.d]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT ##容許3128端口
[root@squid init.d]# service squid reload ##重載配置文件
4,在web服務器上安裝http服務
[root@web ~]# systemctl stop firewalld.service ##關閉防火牆
[root@web ~]# setenforce 0
[root@web ~]# yum install httpd -y ##安裝web服務
[root@web ~]# systemctl start httpd.service
使用client訪問web網頁

[root@web ~]# cd /etc/httpd/logs/ ##查看日誌文件
[root@web logs]# vim access_log ##此時是135地址訪問的
5,修改客戶機瀏覽器代理設置

##從新利用客戶機訪問web服務器
[root@web ~]# cd /etc/httpd/logs/ ##查看日誌文件
[root@web logs]# vim access_log ##此時是179代理服務器訪問的
二,透明代理
實驗拓撲

實驗環境
squid服務器 ens33:192.168.13.184
ens36:192.168.10.1 (僅主機模式)
web服務器 192.168.13.151
client 192.168.10.10 (僅主機模式)
1,在squid服務上添加一塊網卡,並設置ip地址

[root@squid ~]# cd /etc/sysconfig/network-scripts/
[root@squid network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
[root@squid network-scripts]# vim ifcfg-ens36 ##修改ens36ip信息
BOOTPROTO=static
##刪除uuid修改33爲36
IPADDR=192.168.10.1
NETMASK=255.255.255.0
[root@squid network-scripts]# service network restart ##重啓網絡服務
[root@squid network-scripts]# vim /etc/sysctl.conf ##開啓路由轉發
net.ipv4.ip_forward=1
[root@squid network-scripts]# sysctl -p ##加載
2,在web服務器上指定靜態路由
[root@web ~]# route add -net 192.168.10.0/24 gw 192.168.13.184 ##添加靜態路由
3,在squid服務器上設置透明代理
[root@squid network-scripts]# vim /etc/squid.conf ##設置配置文件
http_port 192.168.10.1:3128 transparent ##設置透明代理
cache_effective_user squid
cache_effective_group squid
[root@squid network-scripts]# service squid stop ##關閉開啓squid服務
[root@squid network-scripts]# service squid start
[root@squid network-scripts]# iptables -F ##清空表緩存
[root@squid network-scripts]# iptables -t nat -F
[root@squid network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.10.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
##定義規則入口ens36,80端口重定向到3128
[root@squid network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.10.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
##https443端口
[root@squid network-scripts]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
##容許3128端口訪問
4,用測試機測試

5,在web服務器上查看訪問日誌文件
[root@web ~]# cd /var/log/httpd/
[root@web httpd]# vim access_log ##查看訪問日誌信息

##此時是184訪問的並非測試機的地址訪問的
謝謝閱讀!!