做为应用层的代理服务软件,squid主要提供缓存加速和应用层过滤控制的功能。css
实验环境表:html
1.编译安装Squidlinux
配置Squid的编译选项时,将安装目录设置为/usr/local/squid,其余具体选项根据实际须要来肯定,配置前可参考./configure --help给出的说明。web
tar zxvf squid-3.4.6.tar.gz -C /opt/ cd /opt/squid-3.4.6 ./configure --prefix=/usr/local/squid \ #安装目录 --sysconfdir=/etc \ #单独将配置文件修改到其余目录 --enable-arp-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/
2.squid的配置文件vim
squid服务的配置文件位于/etc/squid.conf,充分了解配置行的做用将有助于管理员根据实际状况灵活地配置代理服务。浏览器
vim /etc/squid.conf http_port 3128 #用来指定代理服务监听的地址和端口(默认端口号为3128) cache_effective_user squid #添加 指定程序用户 cache_effective_group squid #添加 指定帐号基本组
3.squid运行控制缓存
(1)检查配置文件的语法是否正确bash
squid -k parse
(2)启动、中止squid服务器
第一次启动squid服务时,会自动初始化缓存目录。在没有可用的squid服务器脚本的状况下,也能够直接调用squid程序来启动服务,这时须要先进行初始化。
squid -z //初始化缓存目录 squid //启动服务
(3)使用squid服务脚本
为了使squid服务的启动、中止、重载等操做更加方便,能够编写squid服务脚本,并使用chkconfig和service工具来进行管理。
cd /etc/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 chmod +x squid chkconfig --add squid #添加为系统服务 chkconfig --level 35 squid on
1.squid服务器的配置
配置squid实现传统代理服务时,须要注意添加http_access allowall访问策略,以便容许任意客户机使用代理服务。
1)修改squid.conf配置文件
[root@localhost init.d]# vim /etc/squid.conf http_access allow all http_access deny all http_port 3128 cache_mem 64 MB #指定缓存功能所使用的内存空间大小,便于保持访问较频繁的WEB对象,容量最好为4的倍数,单位为MB,建议设为物理内存的1/4 reply_body_max_size 10 MB #容许用户下载的最大文件大小,以字节为单位。默认设置0表示不进行限制 maximum_object_size 4096 KB #容许保存到缓存空间的最大对象大小,以KB为单位,超过大小限制的文件将不被缓存,而是直接转发给用户
2)在防火墙上添加容许策略:
[root@localhost init.d]# iptables -F [root@localhost init.d]# setenforce 0 [root@localhost init.d]# iptables -I INPUT -p tcp --dport 3218 -j ACCEPT [root@localhost init.d]# service squid reload #从新加载
2.客户机的代理配置
在Windows系统中开启浏览器
Internet选项---》链接----》局域网设置----ip:squid服务器地址 端口:3128
地址栏输入web服务器地址。
3.代理服务器的验证
在客户机中经过浏览器访问目标Web服务器网站http://192.168.126.178/,而后观察squid代理服务器,Web服务器的访问日志,以验证代理服务是否发挥做用。
1)查看squid访问日志的新增记录
在squid代理服务器中,经过跟踪squid服务的访问日志文件,应该可以发现客户机192.168.126.177访问网站服务器192.168.126.178的记录。
[root@localhost init.d]# tail /usr/local/squid/var/logs/access.log 1532758490.972 5 192.168.126.177 TCP_MISS/404 512 GET http://192.168.126.178/noindex/css/fonts/Light/OpenSans-Light.eot? - HIER_DIRECT/192.168.126.178 text/html 1532758490.974 5 192.168.126.177 TCP_MISS/404 524 GET http://192.168.126.178/noindex/css/fonts/LightItalic/OpenSans-LightItalic.eot? - HIER_DIRECT/192.168.126.178 text/html
2)查看Web访问日志的新增记录
在被访问的Web服务器中,经过跟踪httpd服务器的访问日志文件,应该可以发现来在代理服务器192.168.126.138的访问记录。说明当前客户机使用代理后,Web服务器并不知道客户机的真实地址,其实是由代理服务器在替它访问。
[root@localhost ~]# cd /etc/httpd/logs/ [root@localhost logs]# ls access_log error_log [root@localhost logs]# cat access_log 192.168.126.138 - - [28/Jul/2018:14:14:50 +0800] "GET / HTTP/1.1" 403 4897 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" //
1.配置Squid支持透明代理
1)在squid代理服务器上添加双网卡,分别是内网地址192.168.100.1和外网地址12.0.0.1。Web服务器地址改成12.0.0.12外网地址,客户端地址改成192.168.100.100内网地址。
[root@localhost 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 ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 #外网地址 inet 12.0.0.1 netmask 255.255.255.0 broadcast 12.0.0.255
2)修改squid的配置文件,在http_port配置上加上transparent(透明)选项,就能够支持透明代理了。
[root@localhost network-scripts]# vim /etc/squid.conf http_port 192.168.100.1:3128 transparent #只在其中一个IP地址上提供服务 [root@localhost network-scripts]# service squid reload
2.设置iptables的重定向策略
防火墙作重定向操做,将访问本机80、443端口的请求重定向到3128端口
[root@localhost network-scripts]# iptables -F [root@localhost network-scripts]# iptables -F -t nat #清除防火墙规则 [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]# iptables -I INPUT -p tcp --dport 3218 -j ACCEPT
3.验证透明代理
在客户机192.168.100.100中经过浏览器访问目标网站http://12.0.0.12/,而后观察squid代理服务器、Web服务器的访问日志,以验证透明代理是否发挥做用。
squid代理服务器的访问日志: [root@localhost network-scripts]# tail /usr/local/squid/var/logs/access.log 1532761302.140 3 192.168.100.100 TCP_MISS/404 512 GET http://12.0.0.12/noindex/css/fonts/Light/OpenSans-Light.eot? - ORIGINAL_DST/12.0.0.12 text/html 1532761302.143 2 192.168.100.100 TCP_MISS/404 524 GET http://12.0.0.12/noindex/css/fonts/LightItalic/OpenSans-LightItalic.eot? - ORIGINAL_DST/12.0.0.12 text/html 1532761302.146 2 192.168.100.100 TCP_MISS/404 516 GET http://12.0.0.12/noindex/css/fonts/Regular/OpenSans-Regular.eot? - ORIGINAL_DST/12.0.0.12 text/html Web服务器的访问日志: [root@localhost ~]# cd /var/log/httpd/ [root@localhost httpd]# vim access_log 12.0.0.1 - - [28/Jul/2018:15:06:43 +0800] "GET /noindex/css/fonts/ExtraBold/OpenSans-ExtraBold.eot? HTTP/1.1" 404 248 "http://12.0.0.12/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" 12.0.0.1 - - [28/Jul/2018:15:06:43 +0800] "GET /noindex/css/fonts/ExtraBoldItalic/OpenSans-ExtraBoldItalic.eot? HTTP/1.1" 404 260 "http://12.0.0.12/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)"
代理成功