zoukankan      html  css  js  c++  java
  • squid研究记录

    Squid研究记录

    Squid cache(简称为Squid)是一个流行的自由软件(GNU通用公共许可证)的代理服务器和Web缓存服务器。Squid有广泛的用途,从作为网页服务器的前置cache服务器缓存相关请求来提高Web服务器的速度,到为一组人共享网络资源而缓存万维网,域名系统和其他网络搜索,到通过过滤流量帮助网络安全,到局域网通过代理上网。Squid主要设计用于在Unix一类系统运行。

        Squid的发展历史相当悠久,功能也相当完善。除了HTTP外,对于FTP与HTTPS的支援也相当好,在3.0 测试版中也支援了IPv6。

                                                                          ----摘自“开源中国社区”

     

    下面是从安装,到配置的整个过程,并对其中的一些参数进行粗浅的解释

    1、下载安装

        先去http://www.squid-cache.org/去下载源代码包squid-3.1.19.tar.gz

        当前版本为squid-3.1.19

    # tar xvfz squid-3.1.19.tar.gz
    # cd squid-3.1.19

        在目录里执行configure操作,该操作是为了生成makefile文件的,所以有一些参数需要设置

    #./configure --prefix=/usr/local/squid --enable-dlmalloc --enable-gnuregex --enable-removal-policies=heap,lru --enable-icmp --enable-delay-pools --enable-snmp --enable-arp-acl --enable-cahce-digests  --enable-storeio=aufs,ufs  --with-pthreads --enable-default-err-languages="Simplify_Chinese" --enable-epoll --enable-linux-netfilter --disable-ident-lookups --with-maxfd=65536 --enable-kill-parent-hack --enable-follow-x-forwarded-for --enable-stacktraces [--enable-ecap]
    # make
    # sudo make install

             安装完成后,进入/usr/local目录,更改squid目录的owner和grouper,这里可以根据自身需要进行更改,可以是新建一个用户专门用来执行squid,如:

    # groupadd  -g  100  squid
    # useradd   -u  100  -g  100  squid

    或者直接使用目前的用户和用户组。

    # sudo chown {owner}:{group} squid –R

    至此,安装过程已经基本结束。

    2、配置squid

    #缓存机制:
    quick_abort_min 0 KB
    quick_abort_max 0 KB
    #如果cache与缓存空间大小的比例达cache_swap_high时,开始swap操作,当比例降到cache_swap_low时,swap操作停止:
    cache_swap_low 90
    cache_swap_high 95
    #采用的swap方式:
    cache_replacement_policy heap LFUDA
    #大于maximum_object_size的不进行disk存储:
    maximum_object_size 4096 KB
    #大于maximum_object_size的不进行memory存储:
    maximum_object_size_in_memory 4096 KB
    #指定内存的最大使用量(根据机器的内存和其它环境进行设定):
    cache_mem 4096 MB
    #打开SMP系统间的内存共享机制(squid-3.1.19不支持):
    #memory_cache_shared on
    #关闭半链接功能(有时squid会停止发送数据,但接收功能不会停止):
    half_closed_clients off
    #打开内存池以供后期使用:
    memory_pools on
    #关闭收集client的统计信息:
    client_db off
    #开启log信息的缓存功能:
    buffered_logs on
    #DNS相关设置(前者指缓存时间上限,后期指失败DNS解析的TTL):
    positive_dns_ttl 8 hours
    negative_dns_ttl 1 seconds
    #IP的最大缓存数量:
    ipcache_size 20480
    #同cache_swap_low和cache_swap_high:
    ipcache_low 90
    ipcache_high 95
    #FQDN的最大缓存数量:
    fqdncache_size 20480
    #http监听端口:
    http_port 3128
    #打开该选项后,用户的no-cache和reload请求将会更新cache:
    reload_into_ims on
    #设置一个limit,如果request的range超过这个limit,则将request进行透传(相当于not cache):
    range_offset_limit -1
    #打开该选项能够忽略带有no-cache的request的超时机制:
    vary_ignore_expire on
    
    #路径设置:
    #cache资源存储的路径 ufs|aufs Mbytes L1 L2(L1为在squid下有多少个目录存放资源,L2为在每个L1目录下有多少个目录存放资源):
    cache_dir aufs /usr/local/squid/var/cache/squid 8192 16 256
    #coredump保存路径,否则保存在起始目录下:
    coredump_dir /usr/local/squid/var/cache/squid
    #access.log的存放路径(如果不存放可替换为cache_access_log none),该log包含了request的响应结果,哪些MEM_HIT,哪些MEM_MISS等:
    cache_access_log /usr/local/squid/var/logs/access.log squid
    # store.log的存放路径(存储缓存信息的log):
    cache_store_log /usr/local/squid/var/logs/store.log
    
    #用户设置:
    #如果以root权限启动该程序,在启动前转到设置的user和group:
    cache_effective_user squid
    cache_effective_group squid
    
    #要用到的命令设置:
    #squid启动过程中会调用pinger,这里要设置好路径,并打开开关:
    pinger_program /usr/local/squid/libexec/pinger
    pinger_enable on
    
    #连接规则配置:
    #localnet包含了一些基于IP的filter:
    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 localnet src fc00::/7       # RFC 4193 local private network range
    acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines
    acl localnet src 118.192.34.0/24
    #localnet包含了一些基于Port的filter:
    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
    
    #允许localnet定义的链接访问该代理:
    http_access allow localnet
    #禁止非Safe_ports定义的端口被访问:
    http_access deny !Safe_ports
    #禁止非SSL_ports定义的端口被链接:
    http_access deny CONNECT !SSL_ports
    #允许Purge功能:
    acl localhost src 127.0.0.1/32
    acl Purge method PURGE
    http_access allow localhost Purge
    #除上面规定的允许访问的链接,其他都不能访问该代理(一定要有这句话,而且顺序不能变):
    http_access deny all
    
    #squid实现gzip功能配置:
    
    ecap_enable on
    ecap_service gzip_service respmod_precache 0 ecap://www.vigos.com/ecap_gzip
    loadable_modules /usr/local/lib/ecap_adapter_gzip.so
    acl GZIP_HTTP_STATUS http_status 200
    adaptation_access gzip_service allow GZIP_HTTP_STATUS
    
    #这些可以具体了解下acl规则。
    
    
    #下面这个是以ftp开头的链接,过期时间为3168分钟:
    refresh_pattern ^ftp:           1440    20%     10080
    #如果想cache图片资源,cache时间为1天,则可以设置:
    refresh_pattern –i .*\\.img.*:           1440    0%     2880
    refresh_pattern –i .*\\.png.*:           1440    0%     2880
    refresh_pattern –i .*\\.gif.*:            1440    0%     2880
    #如果想cache那些第三方设置了no-cache的资源,可以增加options,如:
    refresh_pattern –i .*\\.img.*:           1440    0%     2880  ignore-no-cache
    refresh_pattern –i .*\\.png.*:           1440    0%     2880  ignore-no-cache
    refresh_pattern –i .*\\.gif.*:            1440    0%     2880  ignore-no-cache
    #除此之外还有很多其他的选项,如ignore-no-store,请参考squid.conf.document。 

    #cache规则配置:

    #这块是非常重要的,关系到哪些资源是需要cache,而哪些资源不能cache的。主要的命令就是refresh_pattern [-i] regexp min percent max [options]:

    #先插入段讲解,关于后面那三个数(min percent max),算法如下:

     min, max的单位是分钟,percent就是百分比。

    (当前时间定义为CURRENT_DATE)
    1) If ((CURRENT_DATE-DATE(
    就是LM里定义的时间)) < min),cache是新鲜的
    2) else if ((CURRENT_DATE-DATE) < (min + (max-min)*percent)
    ,cache是新鲜的
    3) else cache
    是过期的
    cache
    过期就需要从后面server取新鲜内容

    3、运行squid

    先建立disk缓存区:

    # /usr/local/squid/sbin/squid –z

    启动squid:

    # /usr/local/squid/sbin/squid

    查看squid是否启动:

    # ps –ef | grep squid

    如果没有squid进程,则可以通过查看/usr/local/squid/var/logs/cache.log来查看原因,如有pinger相关的错误,则需要更改pinger权限,且看conf文件中是否已经设置了pinger的路径:

    # sudo chown root:root /usr/local/squid/libexec/pinger
    # sudo chmod u+s /usr/local/squid/libexec/pinger 

    4、squid其他命令

    1、关闭squid

    /usr/local/squid/sbin/squid –k shutdown

    2、重新载入config文件

    /usr/local/squid/sbin/squid –k reconfigure

    可以通过/usr/local/squid/sbin/squid –k parse来检查config文件配置是否正确。

    3、查看squid的运行信息

    获取cpu、内存、cache比例等数据:

    /usr/local/squid/bin/squidclient -p 80 mgr:info

    /usr/local/squid/bin/squidclient -p 80 cache_object://localhost/info

    从cache中删除某个对象

    /usr/local/squid/bin/squidclient -p 80 -m PURGE http://www.linux.com.cn/

    往cache中添加某个对象

    /usr/local/squid/bin/squidclient -p 80 http://www.linux.com.cn/

    获取内存使用的详细信息

    # /usr/local/squid/bin/squidclient -p 80 mgr:mem

    获取cache对象列表(慎用,可能会导致squid崩溃)

    # /usr/local/squid/bin/squidclient -p 80 mgr:objects

    获取磁盘使用数据

    # /usr/local/squid/bin/squidclient -p 80 mgr:diskd

    4、清空cache

    # /usr/local/squid/sbin/squid –k shutdown
    # mv  /usr/local/squid/var/cache /usr/local/squid/var/cache-old
    # mkdir cache
    # /usr/local/squid/sbin/squid –z
    # /usr/local/squid/sbin/squid
    #rm –rf /usr/local/squid/var/cache-old 

    5、透明代理设置

    在conf文件中将http_port的设置改为

    http_port 3128 transparent

    使用iptables 做nat,并将所有的80请求重定向到本机的3128 以实现透明代理[摘自其他地方]。 
    vi /data/myscripts/squid.firewall
    脚本如下:

    #!/bin/bash
    echo "1" > /proc/sys/net/ipv4/ip_forward
    echo "starting iptables rules"
    /sbin/iptables -t filter -F
    /sbin/iptables -t filter -Z
    /sbin/iptables -t filter -X
    /sbin/iptables -t nat -F
    /sbin/iptables -t nat -Z
    /sbin/iptables -t nat -X
    /sbin/modprobe ip_tables
    /sbin/modprobe iptable_nat
    /sbin/modprobe iptable_filter
    /sbin/modprobe ip_nat_ftp
    /sbin/modprobe ip_conntrack
    /sbin/modprobe ip_conntrack_ftp
    /sbin/iptables -t filter -P INPUT ACCEPT
    /sbin/iptables -t filter -P OUTPUT ACCEPT
    /sbin/iptables -t filter -P  FORWARD ACCEPT
    /sbin/iptables -t nat -A  POSTROUTING -s 192.168.1.0/24 -j SNAT --to-source a.b.c.d
    /sbin/iptables -t nat -A  PREROUTING  -s 192.168.1.0/24 -p tcp --dport 80 -j REDIRECT --to-ports  3128

    执行脚本并将其加入到开机自启动,注意将脚本中的a.b.c.d换成自己公司的公网静态ip

    # ./squid.firewall
    # echo "./squid.firewall" >>/etc/rc.d/rc.local 

    6、将squid添加到开机自启动

    # echo "/usr/local/squid/sbin/squid -s " >>/etc/rc.d/rc.local 

    7、查看端口占用情况并杀掉对应进程:

    # netstat –a
    # lsof –i:3128   //查看占用该端口的进程id

    8、情况iptables

     iptables -t nat -F
     iptables -t nat -X
     iptables -t nat -P PREROUTING ACCEPT
     iptables -t nat -P POSTROUTING ACCEPT
     iptables -t nat -P OUTPUT ACCEPT
     iptables -t mangle -F
     iptables -t mangle -X
     iptables -t mangle -P PREROUTING ACCEPT
     iptables -t mangle -P INPUT ACCEPT
     iptables -t mangle -P FORWARD ACCEPT
     iptables -t mangle -P OUTPUT ACCEPT
     iptables -t mangle -P POSTROUTING ACCEPT
     iptables -F
     iptables -X
     iptables -P FORWARD ACCEPT
     iptables -P INPUT ACCEPT
     iptables -P OUTPUT ACCEPT
     iptables -t raw -F
     iptables -t raw -X
     iptables -t raw -P PREROUTING ACCEPT
     iptables -t raw -P OUTPUT ACCEPT

    5、squid运行中的问题

    1、Your cache is running out of filedescriptors

    sudo -s

    ulimit -HSn 65536

    在configure时加入 --with-maxfd=65536

    不知道在squid.conf加入max_filedescriptors 15000是否起作用

    6、squid实现gzip压缩,先要安装下列包

    1、Download and install libecap Library

     http://www.e-cap.org/Downloads

    cd libecap-0.0.3/
    ./configure
    make
    make install

    2、Download and install VIGOS eCAP GZIP Adapter

    http://code.google.com/p/squid-ecap-gzip/downloads/list

    cd squid-ecap-gzip/
    ./configure
    make
    make install

  • 相关阅读:
    React生命周期, 兄弟组件之间通信
    React组件式编程Demo-用户的增删改查
    React之this.refs, 实现数据双向绑定
    CCF CSP 201812-4 数据中心
    CCF CSP 201812-4 数据中心
    PAT 顶级 1020 Delete At Most Two Characters (35 分)
    PAT 顶级 1020 Delete At Most Two Characters (35 分)
    Codeforces 1245C Constanze's Machine
    Codeforces 1245C Constanze's Machine
    CCF CSP 201712-4 行车路线
  • 原文地址:https://www.cnblogs.com/geekma/p/2625163.html
Copyright © 2011-2022 走看看