之前客户的局域网络用的代理服务软件是ISA2004,系统环境是Windows2003 EE,但是随着终端用户的逐步增多,而且现用的服务器DELL1950配置实在太低,系统运行一段时间就会报虚拟内存过低,不知道什么时间就停机了,而且他们的系统和ISA是D版的(不过话说回来ISA真的是一个非常棒的软件,功能非常的强悍),客户公司短期内没有升级这台服务器的预算,其实这台服务器如果运行linux的话还是非常不错的,所以在与客户的网络管理员分析过后决定用Squid
网络环境:域环境局域网络,多网络权限
有部分终端用户只能发邮件(邮件服务器是Exchange,邮件端口TCP 443),有的部门只能在工作时间上网,其它的服务器都不能与外网联连,但是所有的终端保括服务器都能update病毒库,不过update时间不同,终端用户不能使用任何即时通信工具(QQ……太难封了)。
硬件环境: DELL PowerEdge 1950 E5410
CPU:Quad-Core Xeon Pro E5410
MEM:1GB (2x512MB), 667MHz, ERROR
DISK:73GB, 2.5'', 10Krpm, SAS Hard Drive, Hotplug
系统环境:CentOS6.2_x64(minimal)+Squid

第一步:配置网卡
编辑第一块网卡,也就是接外网的
vim /etc/sysconfog/network-scripts/ifcfg-p4p1
加入
DEVICE="p4p1"
NM_CONTROLLED="yes"
ONBOOT=yes
HWADDR=00:23:AE:5C:B0:E7
TYPE=Ethernet
BOOTPROTO=none
IPADDR=10.8.83.28 #外网IP地址
PREFIX=24 #子网掩码
GATEWAY=10.8.83.254 #网关
DNS1=202.106.0.20 #DNS1
DNS2=202.106.195.68 #DNS2
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System p4p1"
UUID=5dd47203-fffb-671a-4fd0-4cff98347a3b


编辑第二块网卡,接内部网络的
vim /etc/sysconfog/network-scripts/ifcfg-eth0
加入
DEVICE="eth0"
NM_CONTROLLED="yes"
ONBOOT=yes
HWADDR=00:14:78:31:ED:D2
TYPE=Ethernet
BOOTPROTO=none
IPADDR=192.168.88.254 #内网IP
PREFIX=24 #子网掩码
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03
接内部网络的不用配置网关和DNS,只用IP和子网掩码就可以了
第二步:配置iptables
1、启动路由
echo "1">/proc/sys/net/ipv4/ip_forward

#打开“转发”功能。让数据包可以在不同的网卡间“流动”。
因为每次系统启动时初始化脚本/etc/rc.d/rc.sysinit会读取/etc/sysctl.conf文件的内容,该文件默认值0是禁止ip转发,修改为1即开启ip转发功能,修改过后就马上生效,即内核已经打开ip转发功能。但如果系统重启后则又恢复为默认值0,如果想永久打开需要通过修改/etc/sysctl.conf文件的内容来实现,默认sysctl.conf文件中有一个变量是net.ipv4.ip_forward = 0,将后面值改为1,然后保存文件,只是修改sysctl文件不会马上生效,如果想使修改马上生效可以执行下面的命令: sysctl –p
vim /etc/sysctl.conf
net.ipv4.ip_forward = 0改为 net.ipv4.ip_forward = 1

2、清空Iptables所有表中规则并查看nat表
iptables -F
iptables -t nat -F
iptables -t mangle -F
iptables -t nat –L



3、添加iptables的访问规则-NAT
iptables -t nat -A POSTROUTING -s 192.168.88.0/24 -j SNAT --to-source 10.8.83.28
#内部网络的192.168.88.0/24的ip地址段通过SNAT转成10.8.83.28与外网联络
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
#在NAT表的PREROUTING链加目标动作REDIRECT,将入站的数据包进行重定向,将80号端口重定向成8080
service iptables save
系统提示:
iptables:将防火墙规则保存到 /etc/sysconfig/iptables: [确定]
iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
REDIRECT tcp -- anywhere anywhere tcp dpt:http redir ports 8080

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
SNAT all -- localhost/24 anywhere to:10.8.83.28

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

第三步:安装squid
yum install squid* -y
=============================================

Installing:
squid x86_64 7:3.1.10-1.el6_2.4 base 1.7 M
Installing for dependencies:
perl-DBI x86_64 1.609-4.el6 base 705 k

Transaction Summary
==============================================

Iinstall 2 Package(s)

第三步:编译squid.conf
先备份squid.conf
cp /etc/squid/squid.conf /etc/squid/squid.conf.bak
备不备份都行,其实在squid目录下有一个squid.conf.default文件就是这个conf的备份,这只是我个人习惯
vim /etc/squid/squid.conf
在后面修改及增加以下内容(黑色字体为原有的,栗红字体为增加部分,绿色字体为修改部分)
acl manager proto cache_object
acl localhost src 127.0.0.1/32 ::1
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1

acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
acl pm_equipment src "/etc/squid/ip_server" #设置内网IP列表
acl pm_qiantai src "/etc/squid/ip_special" #设置内网IP列表
acl pm_backup src "/etc/squid/ip_limited" #设置内网IP列表
acl pm_dhcp src "/etc/squid/ip_dhcp" #设置内网IP列表
acl deny_ip dst "/etc/squid/deny_ip" #设置禁止访问外网的IP
acl deny_dns dst "/etc/squid/deny_dns" #设置禁止访问的域名
acl deny_urlpath urlpath_regex -i "/etc/squid/deny_urlpath" #设置禁止访问包含关键字的url,如:\.jpg$
acl deny_url url_regex -i "/etc/squid/deny_url" #设置禁止访问的url,如^
http://
acl deny_urlhead dstdom_regex -i "/etc/squid/deny_urlhead" #设置禁止访问某些关键字开头的网址,如:^game
acl ban_down url_regex -i "/etc/squid/deny_downfile" #设置禁止下载的文件类型,如:\.mp3$ \.vbs$ \.rmvb$
acl server_time time SMTWHFA 01:00-03:00 #设置服务器连网时间

========下面内容不是写在squid.conf内的,只是上面配置的说明=======

注:为了方便日后的管理,acl定义的ip地址段都做为一个文件放到/etc/squid/下,例如/etc/squid/ip_server
touch /etc/squid/ip_server
vim /etc/squid/ip_server

写入所要用到的地址段
192.168.88.1-192.168.88.40
SMTWHFA:表示周日~周一,每个字母表示一天
注意:同一网段内的地址范围不用写子网掩码,而分几个网段的就要写了例如:192.168.88.0/24 172.31.1.0/16或者192.168.88.0-192.168.89.0/24,写法为一网段一行)

=======上面内容不是写在squid.conf内的======


acl SSL_ports port 443
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl Safe_ports port 70 # gopher
acl Safe_ports port 210 # wais
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 280 # http-mgmt
acl Safe_ports port 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http
acl CONNECT method CONNECT


acl pmfsip src 192.168.88.3
acl pmfsrmac arp 00:14:78:31:ED:D2
http_access deny pmfsmac !pmfsip
http_access deny !pmfsmac pmfsip
这是ip与mac的绑定,我本也想把它做到一个文件里,不然几十台服务器一个个写,真要吐了,但一直未能成,这回头再试吧。

http_access allow manager localhost #允许manager访问localhost
http_access deny manager #禁止manager访问

http_access deny !Safe_ports #禁止访问不在Safe_ports里的端口
http_access deny CONNECT !SSL_ports #禁止访问非443,563端口

http_access allow localnet
http_access allow localhost
http_access allow pm_qiantai SSL_ports #允许特殊用户使用443端口,但不能上网
http_access allow pm_equipment server_time #服务器上网时间
http_access deny deny_ip
http_access deny deny_dns
http_access deny deny_url
http_access allow pm-dhcp
http_access deny all

http_port 192.168.88.254:8080 transparent
#squid的http监听端口为8080,目地是把目标端口为80重定向到8080端口(默认是3182,改不改看个人),如果要设置为普通的代理这句写成:
http_port 8080

refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern . 0 20% 4320

#禁止缓存 URL中有包含cgi-bin和https:\\开头的都不缓存,asp、cgi、php等动态脚本不缓存
hierarchy_stoplist -i ^https:\\ ?
hierarchy_stoplist cgi-bin ?
acl QUERY urlpath_regex -i cgi-bin \? \.asp \.php \.jsp \.cgi
acl denyssl urlpath_regex -i ^https:\\
no_cache deny QUERY
no_cache deny denyssl

#Cache setup
cache_dir ufs /var/spool/squid 2048 16 256

#设置缓存根目录为/cache,类型为ufs,缓存区大小为2G,可以有16个二级子目录,每有二级目录有256个三级子目录。
cache_mem 1024 MB #指定缓冲内存大小(根据服务器配置以及需要来配置,一般设置1/3-1/2倍的内存大小即可)

coredump_dir /var/spool/squid

cache_swap_low 90 #最低缓存率百分比
cache_swap_high 95 #最高缓存率百分比,当高速缓存占用到95%时,自动减小到90%
maximum_object_size 4096 KB #设置squid磁盘缓存最大文件,超过4M的文件不保存到硬盘
minimum_object_size 0 KB #设置squid磁盘缓存最小文件
maximum_object_size_in_memory 4096 KB #设置squid内存缓存最大文件,超过4M的文件不保存到内存

#dns_children 10 #设置DNS查询程序的进程数,默认是5,据说最高是32
redirect_children 100 #指定squid应该开启多少重定向进程,默认值是5个进程
ipcache_size 1024 #DNS解析后的IP放在缓存中,可免去重复查询DNS,提高访问速度
ipcache_low 90
ipcache_high 95
fqdncache_size 1024

visible_hostname pmproxy #设置主机名
error_directory /usr/share/squid/errors/zh-cn #设置报错为中文
cache_access_log /var/log/squid/access.log #设置访问日志
cache_log /var/log/squid/cache.log #设置缓存日志
squid logfile_rotate 6 #设置日志轮转,6表示会生成0~6共7个日志文件
cache_mgr itadmin@163.com #设置管理员邮箱
forwarded_for off #不传递被代理地址
via off #不传递代理服务器信息

配置完成后保存,并启动squid
service squid start
init_cache_dir /var/spool/squid... 正在启动 squid:. [确定]

将squid加入系统启动项
chkconfig --level 35 squid on

第四步:日志轮转
首先要启动计划任务cron
/sbin/service crond start
将crond加入到系统启动项
vim /etc/rc.d/rc.local
添加一句
/sbin/service crond start


然后再修改/etc/crontab
vim /etc/crontab
添加一句
0 1 * * * /usr/sbin/squid -k rotate
每天的凌晨1点轮转日志

第五步:额外软件webmin
可以通过webmin对squid服务器进行更方便的界面化管理
wget http://prdownloads.sourceforge.net/webadmin/webmin-1.590-1.noarch.rpm
rpm -ivh webmin-1.590-1.noarch.rpm
warning: webmin-1.590-1.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 11f63c51: NOKEY
Preparing... ########################################### [100%]
Operating system is CentOS Linux
1:webmin ########################################### [100%]
Webmin install complete. You can now login to
http://squid-test:10000/
as root with your root password.
通过浏览器http://ipaddress:10000,打开webmin的服务器-squid代理服务器,管理页面可以看到
p_w_picpath