squid传统代理和透明代理squid代理
简介: 1.标准的代理缓冲服务器
一个标准的代理缓冲服务被用于缓存静态的网页如:html 文件和图片文件等到本地网络上的一台主机上(即代理服务器)。当被缓存的页面被第二次访问的时候,浏览器将直接从本地代理服务器那里获取请求数据而不再向原web 站点请求数据。这样就节省了宝贵的网络带宽,而且提高了访问速度。但是,要想实现这种方式,必须在每一个内部主机的浏览器上明确指明代理服务器的IP 地址和端口号。客户端上网时,每次都把请求送给代理服务器处理,代理服务器根据请求确定是否连接到远程web 服务器获取数据。如果在本地缓冲区有目标文件,则直接将文件传给用户即可。如果没有的话则先取回文件,先在本地保存一份缓冲,然后将文件发给客户端浏览器。 2.透明代理缓冲服务器 透明代理缓冲服务和标准代理服务器的功能完全相同。但是,代理操作对客户端的浏览器是透明的(即不需指明代理服务器的IP 和端口)。透明代理服务器阻断网络通信,并且过滤出访问外部的HTTP (80 端口)流量。如果客户端的请求在本地有缓冲则将缓冲的数据直接发给用户,如果在本地没有缓冲则向远程web 服务器发出请求,其余操作和标准的代理服务器完全相同。对于Linux 操作系统来说,透明代理使用Iptables 或者Ipchains 实现。因为不需要对浏览器作任何设置,所以,透明代理对于ISP (Internet 服务器提供商)特别有用。
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的日志和反向代理#