1. 程式人生 > >squid傳統代理和透明代理

squid傳統代理和透明代理

雙網卡 vpd war mct figure under access oot ipaddr

簡介:
squid 服務
傳統模式 設置代理地址
透明模式 無需設置代理(網關)
代理服務器軟件:
Squid
Nginx

具體實驗操作:
squid 192.168.120.128

web 192.168.120.129

client 192.168.120.133

squid-3.5.23軟件包網址:鏈接:https://pan.baidu.com/s/1MbswuQi7pmuNj6XAZmmiJg 密碼:8xu6
-------------------------手工編譯安裝squid-------------------------------------
[root@localhost ~]#mkdir /abc

[root@localhost ~]#mount.cifs //192.168.100.1/rhel7 /abc
[root@localhost ~]#cd /abc/Y2C
[root@localhost Y2C]#tar zxvf squid-3.5.23.tar.gz -C /opt/
[root@localhost Y2C]#cd /opt/
[root@localhost opt]# yum install gcc gcc-c++ make -y
----------------------------配置-------------------------------------------------
[root@localhost opt]# cd squid-3.5.23/
[root@localhost squid-3.5.23]# ./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

[root@localhost squid-3.5.23]# make && make install

[root@localhost squid-3.5.23]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/
[root@localhost squid-3.5.23]# useradd -M -s /sbin/nologin squid
[root@localhost squid-3.5.23]# chown -R squid.squid /usr/local/squid/var/

[root@localhost squid-3.5.23]# vim /etc/squid.conf
#56行
http_access allow all #在http_access deny all上面插入
#61行
cache_effective_user squid #添加 指定程序用戶
#62行
cache_effective_group squid #添加 指定賬號基本組

[root@localhost squid-3.5.23]# squid -z #初始化緩存目錄
[root@localhost squid-3.5.23]# squid #啟動服務
[root@localhost squid-3.5.23]# netstat -ntap | grep 3128 #查看端口
tcp6 0 0 :::3128 :::* LISTEN 45271/(squid-1)

[root@localhost squid-3.5.23]# cd /etc/init.d/
[root@localhost init.d]# vim squid

#!/bin/bash
#chkconfig: 2345 90 25
PID="/usr/local/squid/var/run/squid.pid"
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"

case "$1" in
start)
netstat -natp | grep squid &> /dev/null
if [ $? -eq 0 ]
then
echo "squid is running"
else
echo "正在啟動 squid..."
$CMD
fi
;;
stop)
$CMD -k kill &> /dev/null
rm -rf $PID &> /dev/null
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ]
then
netstat -natp | 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|status|reload|check|restart}"
;;
esac

------------------------傳統代理-------------------------------------------------

[root@localhost init.d]# chmod +x squid
[root@localhost init.d]# chkconfig --add squid
[root@localhost init.d]# chkconfig --level 35 squid on #開機自啟動
[root@localhost init.d]# service squid stop #關閉
[root@localhost init.d]# service squid start #開啟
[root@localhost init.d]# service squid check #檢查語法

[root@localhost init.d]# vim /etc/squid.conf

61 cache_mem 64 MB #指定緩存功能所使用的內存空間大小,便於保持訪問比較頻繁的WEB
對象,容量最好為4的倍數,單位為MB,建議設為物理內存的1/4
62 reply_body_max_size 10 MB #允許用戶下載的最大文件大小(默認為0,不進行限制)
63 maximum_object_size 4096 KB #允許保存到緩存空間的最大對象大小,以KB為單位,超過
大小限制的文件將不被緩存,而是直接轉發給用戶

[root@localhost init.d]# service squid restart
正在關閉 squid...
正在啟動 squid...
[root@localhost init.d]# iptables -F
[root@localhost init.d]# setenforce 0
[root@localhost init.d]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT

操作完成後,在客戶機上訪問192.168.120.128
在訪問網站前,先設置代理
技術分享圖片
技術分享圖片
到web服務器上查看訪問來源
技術分享圖片
客戶機查看web的網頁可以被squid服務器所代理

==============================================================
----------------------------------------在第二臺上操作-----------------------------------------------
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install httpd -y
[root@localhost ~]# systemctl start httpd

-------------------------------------------設置透明代理-----------------------------------------------
配置雙網卡內網ens33 外網ens36
squid 192.168.100.1 內網 ens33
12.0.0.1 外網 ens36
web 12.0.0.12
client 192.168.100.50

#在web上操作:
[root@localhost logs]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

BOOTPROTO=static
IPADDR=12.0.0.12
NETMASK=255.255.255.0
GATEWAY=12.0.0.1
技術分享圖片

[root@localhost logs]# service network restart
Restarting network (via systemctl): [ 確定 ]

#代理服務器上

[root@localhost logs]#cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]#vim ifcfg-ens33
IPADDR=192.168.100.1
NETMASK=255.255.255.0
技術分享圖片
[root@localhost network-scripts]#cp ifcfg-ens33 ifcfg-ens37
[root@localhost network-scripts]#vim ifcfg-ens37

刪除uidd
BOOTPROTO=static
NAME=ens37
DEVICE=ens37
IPADDR=12.0.0.1
技術分享圖片

[root@localhost network-scripts]#service network restart
技術分享圖片
----------------------------------代理服務器上-------------------------------------------
[root@localhost network-scripts]#echo "1" > /proc/sys/net/ipv4/ip_forward
[root@localhost network-scripts]#iptables -F
[root@localhost network-scripts]#iptables -t nat -F
[root@localhost network-scripts]#setenforce 0
[root@localhost network-scripts]#vim /etc/squid.conf

60 http_port 192.168.100.1:3128 transparent

[root@localhost network-scripts]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
[root@localhost network-scripts]#iptables -t nat -I PREROUTING -i ens33 -s 192.168.100.0/24 -p tcp --dport 443 -j REDIRECT --to 3128

[root@localhost network-scripts]#service squid reload

用客戶機訪問12.0.0.12
(關閉代理設置再訪問)
技術分享圖片
技術分享圖片
訪問後到web上

[root@localhost ~]# cd /etc/httpd/
[root@localhost httpd]# cd logs
[root@localhost logs]# ls
access_log error_log
[root@localhost logs]# vim access_log
技術分享圖片

#squid的傳統代理和透明代理就完成了,下一次再介紹squid的日誌和反向代理#

squid傳統代理和透明代理