Squid iptables代理配置
创始人
2024-07-23 05:01:48
0

Squid+iptables代理配置

  Squid代理服务器简介

  Squid是一种在Linux系统下使用的优秀的代理服务器软件。

  squid不仅可用在Linux系统上,还可以用在AIX、Digital Unix、FreeBSD、HP-UX、Irix、NetBSD、Nextstep、SCO和Solaris等系统上。

  Squid是一个缓存internet数据的一个软件,它接收用户的下载申请,并自动处理所下载的数据。也就是说,当一个用户象要下载一个主页时,它向Squid发出一个申请,要Squid替它下载,然后Squid 连接所申请网站并请求该主页,接着把该主页传给用户同时保留一个备份,当别的用户申请同样的页面时,Squid把保存的备份立即传给用户,使用户觉得速度相当快。

  对于Web用户来说,Squid是一个高性能的代理缓存服务器,可以加快内部网浏览Internet的速度,提高客户机的访问命中率。Squid不仅支持HTTP协议,还支持FTP、gopher、SSL和WAIS等协议。

  实验环境:

  系统 redhat linux 5.4

  外网 eth0:192.168.0.1

  内网 eth1:192.168.1.254

  Squid Cache: Version 3.1.7

  RPM安装,这里就不说明了。

  修改配置文件

  1.   [root@a ~]# cat /etc/squid/squid.conf  
  2.  

#p#

  配置文件说明

  定义acl访问控制元素:ip地址、端口

  1.   acl manager proto cache_object  
  2.  
  3.   acl localhost src 127.0.0.1/32 ::1  
  4.  
  5.   acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1  
  6.  
  7.   acl localnet src 10.0.0.0/8 # RFC1918 possible internal network  
  8.  
  9.   acl localnet src 172.16.0.0/12 # RFC1918 possible internal network  
  10.  
  11.   acl localnet src 192.168.0.0/16 # RFC1918 possible internal network  
  12.  
  13.   acl localnet src fc00::/7 # RFC 4193 local private network range  
  14.  
  15.   acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines  
  16.  
  17.   acl SSL_ports port 443  
  18.  
  19.   acl Safe_ports port 80 # http  
  20.  
  21.   acl Safe_ports port 21 # ftp  
  22.  
  23.   acl Safe_ports port 443 # https  
  24.  
  25.   acl Safe_ports port 70 # gopher  
  26.  
  27.   acl Safe_ports port 210 # wais  
  28.  
  29.   acl Safe_ports port 1025-65535 # unregistered ports  
  30.  
  31.   acl Safe_ports port 280 # http-mgmt  
  32.  
  33.   acl Safe_ports port 488 # gss-http  
  34.  
  35.   acl Safe_ports port 591 # filemaker  
  36.  
  37.   acl Safe_ports port 777 # multiling http  
  38.  
  39.   acl CONNECT method CONNECT  
  40.  
  41.   acl worktime time 8:00-23:59 //定义工作时间  
  42.  
  43.   acl worktime time 00:00-5:59  
  44.  
  45.   http_access allow mynetwork !worktime //只允许非工作时间上网  
  46.  

#p#

  访问控制设置

  1.   http_access allow manager localhost  
  2.  
  3.   http_access deny manager  
  4.  
  5.   # Deny requests to certain unsafe ports  
  6.  
  7.   http_access deny !Safe_ports 禁止非安全端口的访问  
  8.  
  9.   # Deny CONNECT to other than secure SSL ports  
  10.  
  11.   http_access deny CONNECT !SSL_ports  
  12.  
  13.   http_access allow localnet  
  14.  
  15.   http_access allow localhost  
  16.  
  17.   # And finally deny all other access to this proxy  
  18.  
  19.   http_access allow all ***一条http_access设置默认访问规则  
  20.  
  21.   # Squid normally listens to port 3128  
  22.  
  23.   http_port 192.168.1.254:3128 transparent 代理服务器监听的地址及端口,transparent 为透明代理模式  
  24.  
  25.   # We recommend you to use at least the following line.  
  26.  
  27.   hierarchy_stoplist cgi-bin ?  
  28.  
  29.   # Uncomment and adjust the following to add a disk cache directory.  
  30.  
  31.   cache_dir ufs /var/spool/squid 1000 8 128 配置缓存文件的文件格式,缓存大小,缓存目录数  
  32.  
  33.   # Leave coredumps in the first cache dir  
  34.  
  35.   coredump_dir /var/spool/squid  
  36.  
  37.   # Add any of your own refresh_pattern entries above these.  
  38.  
  39.   refresh_pattern ^ftp: 1440 20% 10080  
  40.  
  41.   refresh_pattern ^gopher: 1440 0% 1440  
  42.  
  43.   refresh_pattern -i (/cgi-bin/|\?) 0 0% 0  
  44.  
  45.   refresh_pattern . 0 20% 4320  
  46.  
  47.   visible_hostname 192.168.1.254  
  48.  
  49.   cache_mem 128 MB //squid服务器占用内存大小  
  50.  
  51.   forwarded_for off //不传递被代理地址  
  52.  
  53.   via off //不传递代理服务器信息  
  54.  

  初始化squid代理服务器

  1.   squid -z  
  2.  

  开启路由功能,并将下面的命令写入/etc/rc.d/rc.local 文件,使其开机自动开启路由功能

  1.   echo '1' >/proc/sys/net/ipv4/ip_forward  
  2.  

#p#

  配置iptables防火墙

  自动将http请求转发到代理服务器上

  1.   iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 80 -j REDIRECT --to-ports 3128  
  2.  

  设置源地址映射

 

  1.   iptables -t nat -A POSTROUTING -j SNAT --to-source 192.168.1.254 (外网地址)  
  2.  
  1.   iptables -P INPUT ACCEPT  
  2.  

  启动squid代理服务

  1.   [root@a ~]# service squid restart  
  2.  

  查看squid代理服务器缓存日志

  1.   [root@a ~]# tail -f /var/log/squid/access.log  
  2.  

  个人感觉使用Squid+iptables缓存服务器之后网页访问速度有大幅度的加快。

【编辑推荐】

Iptables 配置指南

iptables常用命令及参数

Iptables 详细介绍

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...