• squid2.7安装与配置


    CleverCode近期研究了一下squid的安装与配置。

    如今总结一下。分享给大家。

    1 简单介绍 

       代理server英文全称是Proxy Server,其功能就是代理网络用户去取得网络信息。

         Squid是一个缓存Internet 数据的软件,其接收用户的下载申请,并自己主动处理所下载的数据。

    当一个用户想要下载一个主页时。能够向Squid 发出一个申请,要Squid 取代其进行下载。然后Squid 连接所申请站点并请求该主页,接着把该主页传给用户同一时候保留一个备份,当别的用户申请相同的页面时,Squid 把保存的备份马上传给用户,使用户认为速度相当快。

    Squid 能够代理HTTP、FTP、GOPHER、SSL和WAIS等协议而且Squid 能够自己主动地进行处理,能够依据自己的须要设置Squid。使之过滤掉不想要的东西。


    1.1 工作流程

    当代理server中有client须要的数据时:
    a. client向代理server发送数据请求。
    b. 代理server检查自己的数据缓存。
    c. 代理server在缓存中找到了用户想要的数据,取出数据。
    d. 代理server将从缓存中取得的数据返回给client。

    当代理server中没有client须要的数据时:
    1. client向代理server发送数据请求。
    2. 代理server检查自己的数据缓存。
    3. 代理server在缓存中没有找到用户想要的数据;
    4. 代理server向Internet 上的远端server发送数据请求;
    5. 远端server响应,返回对应的数据;
    6. 代理server取得远端server的数据,返回给client,并保留一份到自己的数据缓存中。




    Squid代理server工作在TCP/IP的应用层。






    1.2 Squid 分类

    依照代理类型的不同。能够将Squid 代理分为正向代理和反向代理。正向代理中,依据实现方式的不同,又能够分为普通代理和透明代理。

    普通代理:须要客户机在浏览器中指定代理server的地址、port。


    透明代理:适用于企业的网关主机(共享接入Internet)中。客户机不须要指定代理server地址、port等信息,代理server须要设置防火墙策略将客户机的Web訪问数据转交给代理服务程序处理。


    反向代理:是指以代理server来接受internet上的连接请求,然后将请求转发给内部网络上的server。并将从server上得到的结果返回给internet上请求连接的client,此时代理server对外就表现为一个server。


    2 系统环境

    操作系统:CentOS release 6.5 (Final)
    Squid版本号:squid-2.7.STABLE9-20101125.tar.gz
    本次安装squid软件下载地址:

    http://download.csdn.net/detail/clevercode/9337437。


    3 安装步骤 

    安装前确保进行安装Linux常备支持库,否则会出现各种错误!

    Linux中必备经常使用支持库的安装具体解释:http://blog.csdn.net/clevercode/article/details/45438401。

    1)解压squid-2.7.STABLE9-20101125.tar.gz
    # cd /usr/local/src/squid
    # tar zxvf squid-2.7.STABLE9-20101125.tar.gz

    2)进入解压后的文件夹
    # cd squid-2.7.STABLE9-20101125

    3)配置
    # ./configure --prefix=/usr/local/squid
    --enable-gnuregex --disable-carp --enable-async-io=240
    --with-pthreads --enable-storeio=ufs,aufs,diskd
    --disable-wccp --enable-icmp --enable-kill-parent-hack
    --enable-cachemgr-hostname=localhost
    --enable-default-err-language=Simplify_Chinese
    --with-maxfd=65535 --enable-epoll
    --enable-linux-netfilter --enable-large-cache-files
    --enable-default-hostsfile=/etc/hosts --with-dl
    --with-large-files --enable-delay-pools --enable-snmp
    --enable-arp-acl --prefix=/usr/local/squid

    4) 编译
    #  make

    5) 安装
    #  make install

    6)配置用户属组 创建、初始化文件夹:
    # groupadd  www  #加入www组    
    # useradd -g  www www -s /bin/false  #创建nginx执行账户www并增加到www组,不同意www用户直接登录系统

    # mkdir -p /data0/cache/logs/
    # chmod 755 /data0/cache/ -R
    # chown www.www /data0/cache -R


    4 实际应用


    4.1 普通代理服务

    即标准的、传统的代理服务,须要客户机在浏览器中指定代理server的地址、port。


    请參考:http://www.cnblogs.com/mchina/p/3812190.html。

    4.2 透明代理服务

    适用于企业的网关主机。客户机不须要指定代理server地址、port等信息,通过iptables将客户机的Web訪问数据转交给代理服务程序处理。


    请參考:http://www.cnblogs.com/mchina/p/3812190.html。

    4.3反向代理服务

    为Internet用户訪问企业Web网站提供缓存加速。


    4.3.1 需求背景

    以下实验将模拟通过不同的域名訪问不同的机器,简单实现企业应用中的负载均衡。
    client在浏览器地址栏中输入pic.domain.com。pic2.domain.com,将訪问192.168.142.130这台机器,
    訪问res.domain.com res2.domain.com,将訪问192.168.142.131这台机器。


    squidserver:192.168.142.133。

    webserver:192.168.142.130 与192.168.142.131。



    4.3.2 配置squid.conf

    # cd /usr/local/squid/etc/
    # mv squid.conf squid.conf.bak

    # vi /usr/local/squid/etc/squid.conf

    #主机名
    visible_hostname   static.squid-133
    
    #设置监听的IP与端口号
    http_port 80 vhost vport
    
    #额外提供给squid使用的内存。squid的内存总占用为 X * 10+15+“cache_mem”,当中X为squid的cache占用的容量(以GB为单位)
    #比方以下的cache大小是100M,即0.1GB。则内存总占用为0.1*10+15+64=80M。推荐大小为物理内存的1/3-1/2或很多其它。

    cache_mem 4096 MB #设置squid内存缓存最大文件。超过2M的文件不保存到内存 maximum_object_size_in_memory 2048 KB #使用lru表示:它仅仅替换长时间没有被訪问过的对象 memory_replacement_policy lru #此记录文件用来记录缓冲对象的加入。删除情况(一般可设置为:cache_store_log none, #来减少日志文件占用的磁盘空间。这一点请特别注意。

    ) cache_store_log none #log文件日志格式 logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh #定义squid的cache存放路径 、cache文件夹容量(单位M)、一级缓存文件夹数量、二级缓存文件夹数量 cache_dir aufs /data0/cache 20480 32 256 #关闭訪问日志 access_log none #此文件记录SQUID PROXY启动,关闭以及代理服务器系统的相关的信息包括系统活动记录 cache_log /data0/cache/logs/cache.log #进程id保存文件 pid_filename /data0/cache/logs/squid.pid #同意最大打开文件数量,0 无限制 max_open_disk_fds 0 #允午最小文件请求体大小 minimum_object_size 0 KB #允午最大文件请求体大小 maximum_object_size 32768 KB #add for gzip server_http11 on cache_vary on acl nginx rep_header Server ^nginx ^fy broken_vary_encoding allow nginx incoming_rate 10 reload_into_ims on acl PURGE method PURGE #允午本机IP acl localhost src 127.0.0.1 192.168.142.0/24 #同意localhost使用该代理 http_access allow PURGE localhost #http_access deny PURGE #用于确定一个页面进入cache后,它在cache中停留的时间(refresh_pattern [-i] regexp min percent max [options]) refresh_pattern -i .swf$ 1440 50% 129600 reload-into-ims refresh_pattern -i .css$ 1440 50% 129600 reload-into-ims #ignore-reload override-expire ignore-no-cache ignore-private override-lastmod refresh_pattern -i .xml$ 1440 50% 129600 reload-into-ims refresh_pattern -i .shtml$ 1440 90% 129600 reload-into-ims #refresh_pattern -i .jpg$ 1440 90% 129600 ignore-reload override-expire ignore-no-cache ignore-private override-lastmod refresh_pattern -i .jpg$ 1440 90% 129600 reload-into-ims refresh_pattern -i .png$ 1440 90% 129600 ignore-reload override-expire ignore-no-cache ignore-private override-lastmod #refresh_pattern -i .png$ 1440 90% 129600 reload-into-ims refresh_pattern -i .gif$ 1440 90% 129600 ignore-reload override-expire ignore-no-cache ignore-private override-lastmod refresh_pattern -i .bmp$ 1440 90% 129600 ignore-reload override-expire ignore-no-cache ignore-private override-lastmod refresh_pattern -i .js$ 1440 90% 129600 ignore-reload override-expire ignore-no-cache ignore-private override-lastmod #refresh_pattern -i .js$ 1440 90% 129600 reload-into-ims #从client过来的请求,假设是 pic.domain.com,pic2.domain.com则Squid向 Server 192.168.142.130的端口80发送请求。 cache_peer 192.168.142.130 parent 80 0 no-query no-digest originserver name=pic cache_peer_domain pic pic.domain.com pic2.domain.com #从client过来的请求。假设是 res.domain.com。res2.domain.com则Squid向 Server 192.168.142.131的端口80发送请求; cache_peer 192.168.142.131 parent 80 0 no-query no-digest originserver name=res cache_peer_domain res res.domain.com res2.domain.com #同意全部IP訪问 acl all src 0.0.0.0/0.0.0.0 http_access allow all #同意全部的http报头 header_access Via deny all #此标记设置snmp_port端口为3401,能够使MRTG监測服务执行状态 acl CactiServer src 192.168.142.93 acl SNMP snmp_community nihao@monitor snmp_port 3401 snmp_access allow SNMP CactiServer snmp_access deny all #上面几个就是说遇到URL中有包括cgi-bin和以avi等结尾的都不要缓存, acl QUERY urlpath_regex cgi-bin .cgi$ .avi$ .wmv$ .rm$ .ram$ .mpg$ .mpeg$ .zip$ .exe$ cache deny QUERY #acl PURGE method PURGE #acl localhost src 172.16.218.0/32 #http_access allow PURGE localhost #带问号的不缓存 refresh_pattern ? 0 100% 0 ignore-reload #5h refresh_pattern .php?keyword 240 50% 720 refresh_pattern .php?housetag 3 50% 6 refresh_pattern .html 5 50% 30 #以root来启动squid,而且没有添加cache_effective_user行,那么squid默认以nobody用户执行 cache_effective_user www cache_effective_group www


    4.3.3 初始化配置

    # /usr/local/squid/sbin/squid -z

    看到下面提示初始化正常。
    2015/12/08 04:03:34| Creating Swap Directories

    4.3.4 启动

    # 后台启动 

    # /usr/local/squid/sbin/squid -s 


    4.3.5 訪问

    1)通过訪问http://res.domain.com/comm.js。能够到看到res.domain.com是指向了192.168.142.133。可是实际取出js文件是从192.168.142.131server中取得。



    2)改动comm.js 成为例如以下内容。可是发现訪问comm.js内容还是没有变。

    #vi comm.js

    this is from 192.168.142.131 js! i am change !



    3)清除133缓存。再訪问数据就变了。

    # /usr/local/squid/bin/squidclient -m PURGE -p 80 "http://res.domain.com/comm.js"



    4)改变URL也会是数据变化。squid缓存的key能够理解为url的md5。

    仅仅要url发生了变化。那么数据就会发生编号。

    如图訪问:http://res.domain.com/comm.js?r=123456。



    5 )訪问图片


    5 squid经常使用管理命令

    # /usr/local/squid/sbin/squid -z 初始化缓存空间
    # /usr/local/squid/sbin/squid   前台启动 (用于输出调试)
    # /usr/local/squid/sbin/squid -s 后台启动 
    # /usr/local/squid/sbin/squid -k shutdown   停止
    # /usr/local/squid/sbin/squid -k reconfigure  又一次加载配置文件
    # /usr/local/squid/sbin/squid -k parse       检查conf
    # /usr/local/squid/sbin/squid -k rotate  优化碎片

    # /usr/local/squid/bin/squidclient -p 80 mgr:info                          查看squid命中情况
    # /usr/local/squid/bin/squidclient -m PURGE -p 80 "http://res.domain.com/comm.js"  删除指定URL缓存



  • 相关阅读:
    Poj3678:Katu Puzzle
    2-SAT
    Bzoj3238: [Ahoi2013]差异
    expressJS
    expressJS
    expressJS
    [转]View属性 之 paddingStart & paddingEnd
    在Activity之间使用Intent传值和Bundle传值的区别和方式
    [转]Java初始化顺序总结
    final关键字修饰的变量
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/7352617.html
Copyright © 2020-2023  润新知