Squid代理服務器——傳統代理,透明代理(實踐!)

緩存代理概述

web代理的工作機制:
緩存網頁對象,減少重複請求

Squid代理服務器——傳統代理,透明代理(實踐!)

代理的基本類型

傳統代理:適用於Internet,需明確指定服務端
透明代理:客戶機不需要指定代理服務器的地址和端口,是通過默認路由,防火牆將web重定向給代理

使用代理的好處

提高web訪問速度
隱藏客戶機的真實IP地址

實驗環境

squid服務器:192.168.52.134
web服務器:192.168.52.135
client服務器:192.168.52.138

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

5、測試代理服務

(1)使用client測試機直接訪問web網頁(訪問完畢清除緩存)

Squid代理服務器——傳統代理,透明代理(實踐!)

(2)設置代理再次訪問

Squid代理服務器——傳統代理,透明代理(實踐!)
Squid代理服務器——傳統代理,透明代理(實踐!)
Squid代理服務器——傳統代理,透明代理(實踐!)
Squid代理服務器——傳統代理,透明代理(實踐!)
Squid代理服務器——傳統代理,透明代理(實踐!)

(3)查看web主機的httpd服務日誌文件

[root@localhost logs]# head -1 access_log 
192.168.52.138 - - [04/Dec/2019:17:09:15 +0800] "GET / HTTP/1.1" 403 4897 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240"
#可以看到是client主機的IP地址訪問的
[root@localhost logs]#

[root@localhost logs]# tail -1 access_log 
192.168.52.134 - - [04/Dec/2019:17:26:55 +0800] "GET /browserconfig.xml HTTP/1.1" 404 215 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240"
#可以看到是squid服務器的IP地址訪問的
[root@localhost logs]#

二、透明代理

實驗環境:

squid:192.168.100.1  內網:ens33
            12.0.0.1           外網:ens36
web:12.0.0.12
client:192.168.100.50

1、在squid服務上添加一塊網卡,並設置ip地址

Squid代理服務器——傳統代理,透明代理(實踐!)

ens33網卡配置文件:
[root@squid ~]# cd /etc/sysconfig/network-scripts/
[root@squid network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=8ecd53ce-afdb-46f8-b7ff-b2f428a3bc8f
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.100.1
NETMASK=255.255.255.0
ens36網卡配置文件:
[root@squid network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
[root@squid network-scripts]# vim ifcfg-ens36
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens36
DEVICE=ens36
ONBOOT=yes
IPADDR=12.0.0.1
NETMASK=255.255.255.0
[root@squid network-scripts]# systemctl restart network
[root@squid network-scripts]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.1  netmask 255.255.255.0  broadcast 192.168.100.255
        inet6 fe80::c776:9d00:618:88f2  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:eb:34:07  txqueuelen 1000  (Ethernet)
        RX packets 226  bytes 31111 (30.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 84  bytes 10776 (10.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 12.0.0.1  netmask 255.255.255.0  broadcast 12.0.0.255
        inet6 fe80::55bc:65c1:7046:e2d6  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:eb:34:11  txqueuelen 1000  (Ethernet)
        RX packets 216  bytes 30613 (29.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 76  bytes 9847 (9.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 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、在squid服務器上設置透明代理

[root@squid network-scripts]# vim /etc/squid.conf   #設置配置文件
http_port 192.168.100.1:3128    transparent   #設置透明代理

[root@squid ~]# iptables -F   #清空表緩存
[root@squid ~]# iptables -t nat -F    #清空NAT表緩存
root@squid ~]# setenforce 0    #關閉增強型安全功能
[root@squid network-scripts]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
#定義規則入口ens33,80端口重定向到3128
[root@squid network-scripts]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.100.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端口訪問
[root@squid network-scripts]# service squid stop    #關閉服務
[root@squid network-scripts]# service squid start    #開啓服務
正在啓動 squid...
[root@squid network-scripts]# 

3、分別將web主機和client測試主機網絡模式改爲僅主機

Squid代理服務器——傳統代理,透明代理(實踐!)

Squid代理服務器——傳統代理,透明代理(實踐!)

4、配置web主機的固定IP地址

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=3ceed540-b04c-48d6-a4f7-79951f09ea1d
DEVICE=ens33
ONBOOT=yes
IPADDR=12.0.0.12
NETMASK=255.255.255.0
GATEWAY=12.0.0.1
[root@localhost ~]# systemctl restart network
[root@localhost ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 12.0.0.12  netmask 255.255.255.0  broadcast 12.0.0.255
        inet6 fe80::3e1d:31ba:f66a:6f80  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:95:9b:1b  txqueuelen 1000  (Ethernet)
        RX packets 189  bytes 26901 (26.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 83  bytes 10980 (10.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

5、配置client測試主機固定IP地址,並關閉代理服務器

Squid代理服務器——傳統代理,透明代理(實踐!)
Squid代理服務器——傳統代理,透明代理(實踐!)

6、測試代理服務

Squid代理服務器——傳統代理,透明代理(實踐!)

在web服務器查看httpd服務訪問日誌

[root@localhost ~]# tail -1 /etc/httpd/logs/access_log 
12.0.0.1 - - [04/Dec/2019:19:57:53 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240"
#可以看到是代理服務器的IP地址訪問的
[root@localhost ~]# 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章