缓存代理概述:做为应用层的代理服务软件,squid主要提供缓存加速,应用层过滤控制的功能。

1.代理的工作机制

当客户机通过代理来请求web页面时,指定的代理服务器会先检查自己的缓存,如果缓存中已经有客户机需要的页面,则直接将缓存中的页面内容反馈给客户机,如果缓存中没有客户机要访问的页面,则由代理服务器向Internet发送访问请求,当获得返回的web页面以后,将网页数据保存到缓存中并发送给客户机!如图

HTTP代理缓存加速对象主要是文字,图像等静态web元素。使用缓存机制后,当客户机在不同的时候访问同一web元素,或者不同的客户机访问不同的web元素时,可以直接从代理服务器的 缓存中获得结果。这样就大大减少了想Internet提交重复的web请求的过程,提高了客户机的web访问响应速度。

由于客户机的web访问请求实际上是由代理服务器来替代完成的,从而可以隐藏用户的真实ip地址,起到一定的保护作用。另一方面,代理服务器担任着类似“经纪人”的角色,所以有机会针对要访问的目标,客户机的地址,访问的时间段等进行过滤控制。

2.代理的基本性质类型。

根据实现方式不同,代理服务器可分为传统代理和透明代理两种常见的代理服务。

1)传统代理:也就是普通的代理服务,首先必须在客户机的浏览器,qq聊天工具,下载软件等程序中手动设置代理服务器的地址和端口,然后才能使用代理来访问网络。对于网页浏览器,访问网站时的域名解析请求也会发给指定的代理服务器。

2)透明代理:提供与传统代理相同的功能和服务,其区别在于客户机不需要指定代理服务器的地址和端口,而是通过默认路由,防火墙策略将web访问重定向,实际仍然交给代理服务器处理。重定向的过程对客户机来说是透明的,用户甚至并不知道自己在使用代理服务,所有称为“透明代理”,使用透明代理时,网页浏览器访问网站时的域名解析请求将优先先发给dns服务器。

安装及运行控制

软件:squid3.4.6

系统:centos 6.5

1)编译安装squid

[root@localhost ~]# tar zxf squid-.tar.gz -C /usr/src/   //解压
[root@localhost ~]# cd /usr/src/squid-/
[root@localhost squid-]# ./configure --prefix=/usr/local/squid --sysconfdir=/etc --enable-arp-acl --enable-linux-netfilter --enable-linux-tproxy --enable- --enable-err-language="Simplify_Chinese" --enable-underscore --enable-poll --enable-gnuregex     //配置
[root@localhost squid-]# make && make install     //编译安装
  • --prefix=/usr/local/squid          //安装目录
  • --sysconfdir=/etc                   //单独将配置文件修改到其他目录
  • --enable-arp-acl                    //可以在规则中设置直接通过客户端MAC进行管理,防止客户端使用ip欺骗
  • --enable-linux-netfilter           //使用内核过滤
  • --enable-linux-tproxy            //支持透明模式
  • --enable-async-io                 //异步i/o,提升存储性能,相当于--enable-pthreads     --enable-storeio=ufs,aufs --with//-pthreads  --with-aufs-thread=值
  • --enable-err-language="Simplify_Chinses"    //错误信息的显示语音
  • --enable-underscore                                  //允许URL中有下划线
  • --enable-poll                                            //使用poll()模式,提高性能
  • --enable-gnuregex                                  //使用GNU正则表达式

创建链接文件、创建用户和组更改目录属主属组

[root@localhost squid-]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/
[root@localhost squid-3.4.6]# useradd -M -s /sbin/nologin squid
[root@localhost squid-3.4.6]# chown -R squid.squid /usr/local/squid/var/
[root@localhost squid-3.4.6]# vim /etc/squid.conf     //修改配置文件
49 # Example rule allowing access from your local networks.
 50 # Adapt localnet in the ACL section to list your (internal) IP networks
 51 # from where browsing should be allowed
 52 http_access allow localnet
 53 http_access allow localhost
 54
 55 # And finally deny all other access to this proxy
 56 http_access deny all
 57
 58 # Squid normally listens to port 3128
 59 http_port 3128
 60 cache_effective_user squid   //这一项指定squid的程序用户,用来设置初始化允许缓存的账号,负责启动不成功!
 61 cache_effective_group squid   //默认为cache_effective_user指定账号的基本组
 62 # Uncomment and adjust the following to add a disk cache directory.
 63 #cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256
 64
 65 # Leave coredumps in the first cache dir
 66 coredump_dir /usr/local/squid/var/cache/squid
 67
[root@localhost ~]# squid -k parse    //检查配置文件语法是否正确
// ::| Startup: Initializing Authentication Schemes ...
// ::| Startup: Initialized Authentication Scheme 'basic'
// ::| Startup: Initialized Authentication Scheme 'digest'
// ::| Startup: Initialized Authentication Scheme 'negotiate'
// ::| Startup: Initialized Authentication Scheme 'ntlm'
// ::| Startup: Initialized Authentication.
// ::| Processing Configuration File: /etc/squid.conf (depth )
// ::| Processing: acl localnet src     # RFC1918 possible internal network
// ::| Processing: acl localnet src     # RFC1918 possible internal network
// ::| Processing: acl localnet src     # RFC1918 possible internal network
// ::| Processing: acl localnet src fc00::/       # RFC  local private network range
// ::| Processing: acl localnet src fe80::/      # RFC  link-local (directly plugged) machines
// ::| Processing: acl SSL_ports port
// ::| Processing: acl Safe_ports port         # http
// ::| Processing: acl Safe_ports port         # ftp
// ::| Processing: acl Safe_ports port         # https
// ::| Processing: acl Safe_ports port         # gopher
// ::| Processing: acl Safe_ports port         # wais
// ::| Processing: acl Safe_ports port -    # unregistered ports
// ::| Processing: acl Safe_ports port         # http-mgmt
// ::| Processing: acl Safe_ports port         # gss-http
// ::| Processing: acl Safe_ports port         # filemaker
// ::| Processing: acl Safe_ports port         # multiling http
// ::| Processing: acl CONNECT method CONNECT
// ::| Processing: http_access deny !Safe_ports
// ::| Processing: http_access deny CONNECT !SSL_ports
// ::| Processing: http_access allow localhost manager
// ::| Processing: http_access deny manager
// ::| Processing: http_access allow localnet
// ::| Processing: http_access allow localhost
// ::| Processing: http_access deny all
// ::| Processing: http_port
// ::| Processing: cache_effective_user squid
// ::| Processing: cache_effective_group squid
// ::| Processing: coredump_dir /usr/local/squid/var/cache/squid
// ::| Processing: refresh_pattern ^ftp:            %
// ::| Processing: refresh_pattern ^gopher:        %
// ::| Processing: refresh_pattern -i (/cgi-bin/|\?)     %
// ::| Processing: refresh_pattern .            %
[root@localhost ~]# 

[root@localhost ~]# squid -z       //启动前必须初始化缓存目录

[root@localhost ~]# squid          //启动squid

服务启动脚本:如下

#!/bin/bash
#chkconfig: 2345 90 25
#config: /etc/squid.conf
#pidfile: /usr/local/squid/var/run/squid.pid
#description: squid - internet object cache.
PID="usr/local/squid/var/run/squid.pid"
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"
case "$1" in
start)
    netstat -anpt | 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 -anpt | 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 |restart | reload | check | status}"
;;
esac
[root@localhost ~]# chmod  /etc/init.d/squid     //授权脚本权限
[root@localhost ~]# /etc/init.d/squid restart           //重启squid服务
[root@localhost ~]# chkconfig squid on                //开机自启动
[root@localhost ~]# chkconfig --add squid           //添加系统服务

1.传统代理

在Linux主机B(squid)为客户机访问各种网站提供代理服务,但禁止通过代理下载超过10MB大小的文件。在客户机C上,指定主机B(squid)作为web访问代理,以隐藏自己的真是ip地址。

叙述:主机B最为代理服务,并允许客户机使用代理;如要客户机通过以http://www.baidu.com的域名形式访问,则代理服务器本身必须能够正确解析该域名。客户端需要为浏览器等程序指定使用的代理服务器地址,端口号等信息,网站服务器需要重启httpd服务。

1.squid服务器的配置

配置squid实现传统代理服务时,需要注意添加http_access  allow all 访问策略;便允许任意客户机使用代理服务。除此以外,为了限制下载的文件大小,在需要设置 reply_body_max_size项,其他参数默认即可。

[root@localhost ~]# vim /etc/squid.conf
59 http_port 3128
60 reply_body_max_size 10 MB    //允许下载的最大文件大小(10MB)
61 http_access allow all             //放在http_access_deny all 之前

在防火墙上添加策略

[root@localhost ~]# iptables -I INPUT -p tcp --dport  -j ACCEPT
[root@localhost ~]# service iptables save
iptables:将防火墙规则保存到 /etc/sysconfig/iptables:     [确定]
[root@localhost ~]# 

重启ssquid服务即可生效

[root@localhost ~]# service squid restart
正在关闭squid……
正在启动squid……
[root@localhost ~]# 

2.客户端代理设置

在IE浏览器中,选择工具----Internet选项,选择连接

局域网设置,

若在Linux客户机的命令行中使用代理服务器(如elinks,wget下载工具)请用变量指定代理服务器地址

[root@localhost ~]# vim /etc/profile
HTTP_PROXY=http://172.16.1.100:3128    //为http协议指定代理
HTTPS_PROXT=http://172.16.1.100:3128  //为https协议指定代理
FTP_PROXY=http://172.16.1.100:3128     //为ftp协议指定代理
NO_PROXY=192.168.1.,192.168.2.       //对两个局域网段不使用代理
export HTTP_PROXY HTTPS_PROXY FTP_PROXY NO_PROXT
[root@localhost ~]# source /etc/profile

验证:

在客户机172.16.1.200中通过浏览器访问目标网站http://172.16.1.2 然后观察代理服务器,web服务器的访问日志,以验证代理服务器是否发挥作用

1)查看squid访问日志

[root@localhost ~]# tail /usr/local/squid/var/logs/access.log
   GET http://172.16.1.2/icons/poweredby.png - HIER_DIRECT/172.16.1.2 -
   GET http://172.16.1.2/ - HIER_DIRECT/172.16.1.2 text/html
   GET http://172.16.1.2/icons/poweredby.png - HIER_DIRECT/172.16.1.2 -
   GET http://172.16.1.2/icons/apache_pb.gif - HIER_DIRECT/172.16.1.2 -
   GET http://172.16.1.2/ - HIER_DIRECT/172.16.1.2 text/html
   GET http://172.16.1.2/icons/poweredby.png - HIER_DIRECT/172.16.1.2 -
   GET http://172.16.1.2/icons/apache_pb.gif - HIER_DIRECT/172.16.1.2 -
   GET http://172.16.1.2/ - HIER_DIRECT/172.16.1.2 text/html
   GET http://172.16.1.2/icons/poweredby.png - HIER_DIRECT/172.16.1.2 -

注:可以看出172.16.1.200 访问172.16.1.2网站服务器

2)查看web访问日志的新增记录

[root@localhost ~]# tail /var/log/httpd/access_log
/Sep/::: +]  - "http://172.16.1.2/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
/Sep/::: +]   "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
/Sep/::: +]  - "http://172.16.1.2/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
/Sep/::: +]  - "http://172.16.1.2/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"

注:当客户再次访问web,查看web服务器日志,发现是来自代理服务器172.16.1.100的访问记录,这说明,web服务器并不知道客户机的真实ip,因为实际上由代理服务器替代了

2.透明代理

透明代理提供的服务功能与传统代理时一致的,但是其‘透明’的实现做依赖于默认路由和防火墙的重定向策略,因此更适合为局域网主机服务,而不适合Internet中的客户机提供服务

叙述:透明代理的重在Linux网关服务器,而对客户机仅需要正确配置网络,网关,而并不需要指定代理服务器,关于客户机的dns解析工作,最好还是通过正常的DNS服务器来提供,不建议抛给代理服务器来处理。

1.配置squid支持透明代理

squid服务的默认配置并不支持透明代理,因此需要调整相关设置。添加“transparent”(透明)选项,就可以支持透明代理了

[root@localhost ~]# vim /etc/squid.conf
 # Squid normally listens to port
 http_port  transparent   //只在其中一个ip地址上提供服务

[root@localhost ~]# service squid reload

2.设置iptables的重定向策略

透明代理中的quid服务实际上构建 在Linux网关主机上的,因此只需要正确设置防火墙策略,就可以将局域网主机访问Internet的数据包转交给squid进行处理。这需要用到iptables重定向策略,其 是实现本机端口的重定向,将访问网站协议http,https的外发数据包交给本机的squid服务(3128端口)

PEDIRECT也是一种数据包控制类型,只能在nat表的PREROUTING或OUTPUT链以及被其调用的链中使用,通过‘--to-ports’ 端口号的形式来映射的目标端口。

[root@localhost ~]# iptables -t nat -I PREROUTING -i eth1 -s  -p tcp --dport  -j REDIRECT --to
[root@localhost ~]# iptables -t nat -I PREROUTING -i eth1 -s  -p tcp --dport  -j REDIRECT --to
[root@localhost ~]# service iptables save
iptables:将防火墙规则保存到 /etc/sysconfig/iptables:     [确定]
[root@localhost ~]# 

Squid代理服务器的更多相关文章

  1. CentOS安装squid代理服务器

    Squid是一个Linux系统下优秀的代理服务器软件.Squid可以配置普通上网代理(正向代理).反向代理.透明代理.系统是CentOS6.2.Squid主机IP为192.168.1.100. 安装s ...

  2. 【转载】CentOS 6.4下Squid代理服务器的安装与配置

    一.简介 代理服务器英文全称是Proxy Server,其功能就是代理网络用户去取得网络信息. Squid是一个缓存Internet 数据的软件,其接收用户的下载申请,并自动处理所下载的数据.当一个用 ...

  3. CentOS 6.4下Squid代理服务器的安装与配置

    一.简介 代理服务器英文全称是Proxy Server,其功能就是代理网络用户去取得网络信息. Squid是一个缓存Internet 数据的软件,其接收用户的下载申请,并自动处理所下载的数据.当一个用 ...

  4. Squid代理服务器的安装与配置

    一.简介 代理服务器英文全称是Proxy Server,其功能就是代理网络用户去取得网络信息. Squid是一个缓存Internet 数据的软件,其接收用户的下载申请,并自动处理所下载的数据.当一个用 ...

  5. CentOS6 Squid代理服务器的安装与配置

    代理服务器英文全称是Proxy Server,其功能就是代理网络用户去取得网络信息.Squid是一个缓存Internet 数据的软件,其接收用户的下载申请,并自动处理所下载的数据.当一个用户想要下载一 ...

  6. CentOS 6.4下Squid代理服务器的安装与配置,反向代理

    CentOS 6.4下Squid代理服务器的安装与配置 一.简介 代理服务器英文全称是Proxy Server,其功能就是代理网络用户去取得网络信息. Squid是一个缓存Internet 数据的软件 ...

  7. CentOS 7安装squid代理服务器

    Squid,一个高性能的代理缓存服务器,支持FTP.gopher.HTTP协议. Squid,一个缓存Internet 数据的软件,其接收用户的下载申请(作为代理服务器),并自动处理所下载的数据,并返 ...

  8. linux 搭建squid代理服务器

    linux 搭建squid代理服务器 实验环境: 一台linux搭建Web服务器,充当内网web服务器(同时充当内网客户端) 202.100.10.100 一台linux系统充当网关服务器,两个网卡, ...

  9. CentOS 7 Squid代理服务器反向代理

    Squid反向代理 将不同的URL请求分发到后台不同的Web服务器上,通过squid反向代理,可以加速网站的访问速度,同时互联网用户只能看到反向代理服务器的地址,加强网站的访问安全 Squid反向代理 ...

  10. CentOS 7 Squid代理服务器正向代理-透明代理

    Squid是Linux系统中最常用的一款开源代理服务软件,主要提供缓存加速和应用层过滤控制的功能,可以很好的实现HTTP.FTP.DNS查询以及SSL等应用的缓存代理 透明代理:提供与传统代理相同的功 ...

随机推荐

  1. ENTITYFRAMEWORKCORE 二使用配置文件来配置数据库链接

    首先 配置文件现在已经变成appsettings.json, 先添加一个连接字符串 "ConnectionStrings": { "PWDatabase": & ...

  2. C#中可直接调用WIN32的API函数--USER32.DLL

    Win32的API函数可以直接在C#中直接调用,在做WinForm时还是很有帮助的.有时候直接调用Win32的API,可以很高效的实现想要的效果. using System; using System ...

  3. C++学习笔记25:makefile文件2

    Makefile文件语法 行解析:命令按行解析 命令行的行首字符为Tab键,其他行的行首字符不得为Tab键,但可以使用多个空格缩进 换行:命令太长时,行尾使用"\"换行 注释:行首 ...

  4. python自动化框架nose

    python除了unittest,还有一款更快捷的nose,nose可以说是对unittest的一种简化吧 但是他不需要unittest那种必须有固有的格式,他只需要文件,类名,方法名等含有test就 ...

  5. 【转】Qt下使用glut库

    ps:这个说的很明白,尤其是win10环境下用mingw环境时编程时碰到的问题, 1.加 windows.h 2.在.pro 添加libs     博文地址:Qt下使用glut库   本人使用的环境 ...

  6. The Non-Inverting Amplifier Output Resistance by Adrian S. Nastase [ Copied ]

    Source Address: http://masteringelectronicsdesign.com/the-non-inverting-amplifier-output-resistance/ ...

  7. 条件随机场(CRF) - 2 - 定义和形式(转载)

    转载自:http://www.68idc.cn/help/jiabenmake/qita/20160530618218.html 参考书本: <2012.李航.统计学习方法.pdf> 书上 ...

  8. 第五篇T语言实例开发,百变字符(版本5.0)

    自动生成字符编码 字符的编码 参见下面链接 http://blog.jobbole.com/74109/ http://baike.baidu.com/link?url=QN7HMY2fJTIUfIT ...

  9. leetcode 186. Reverse Words in a String II 旋转字符数组 ---------- java

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  10. Python2.7.12开发环境构建(自动补全)

    一.安装readline-devel包 Python的编译安装依赖于这个包 yum -y install readline-devel 二.安装Python2.7.12 Python官方网站(到此处下 ...