squid 代理分为“正向代理”和“反向代理”。

  “正向代理”主要应用于代理用户去访问外网,起到对用户访问行为的管理及对带宽的节省(/data/cache 目录保存了用户访问的缓存,当其他用户再次访问相同的资源时,会先从/data/cache 里读取)。

  “反向代理”主要应用于代理服务请的请求。例如用户A访问服务器C,速度很慢,但是服务器C与服务器B之间的通讯很快,同时用户A与服务器B之间的通讯也很快。因此可把服务器B设为反向代理服务器,通过代理服务器B来代理服务器C的请求。


一:安装squid

    yum -y install squid

二:配置squid服务

    vim /etc/squid/squid.conf

    

  A:正向代理  (代理用户的请求)

        http_port 2480   #squid的代理端口
        acl manager proto cache_object    #定义的acl访问控制
        acl localhost src 127.0.0.1/32 ::1  #lo的回环
        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 localnet src 192.168.0.0/16 # RFC1918 possible internal network
        acl SSL_ports port 443
        acl Safe_ports port 80 8080         # http的端口
        acl Safe_ports port 21          # ftp的端口
        acl Safe_ports port 443         # https的端口
        acl CONNECT method CONNECT
        http_access allow manager localhost
        http_access deny manager
        http_access deny !Safe_ports
        http_access deny CONNECT !SSL_ports
        http_access allow localnet
        http_access allow localhost
        http_access allow all
        cache_dir aufs /data/cache 1024 16 256   #squid缓存的目录  1024为cache的

        总空间。 16为一层目录的数量,256为二层目录的数量
        cache_mem 128 MB   #内存大小
        hierarchy_stoplist cgi-bin ?
        coredump_dir /var/spool/squid
        refresh_pattern ^ftp:           1440    20%     10080
        refresh_pattern ^gopher:        1440    0%      1440
        refresh_pattern -i (/cgi-bin/|\?) 0     0%      0
        refresh_pattern \.(jpg|png|gif|mp3|xml) 1440    50%     2880            ignore-reload
        refresh_pattern .               0       20%     4320




        【acl 访问控制举例】

            设置http访问的白名单  (只允许访问baidu和zol   dstdomain后面跟域

            名) 相同的思路可设置http访问的黑名单。

                acl http proto HTTP 
                                                                acl pass_domain dstdomain .baidu.com .zol.com 
                                                                http_access allow http pass_domain 
                                                                http_access deny http !pass_domain



三:启动squid

    /etc/init.d/squid start

    squid -kch  :检查squid.conf的配置是否出错(需要先启动squid);

    squid -kre  :重新加载squid


四:在IE浏览器设置代理

    squid 代理服务器的配置_squid代理   

squid 代理服务器的配置_linux_02    

 

squid 代理服务器的配置_linux_03

勾选“为LAN使用代理服务器”地址:squid的IP 端口:squid.conf里设置的端口。




 B:反向代理    (代理服务的请求)

        vim /etc/squid/squid.conf

        

       http_port 80 accel vhost vport 
                            cache_peer 58.215.191.3 parent 80 0 originserver name=a 
                            cache_peer 180.97.33.107 parent 80 0 originserver name=b 
                            cache_peer_domain a nba.hupu.com
                            cache_peer_domain b www.baidu.com


    其他步骤与正向代理一致。