squid安装使用

来源:互联网 发布:软件产品质量控制 编辑:程序博客网 时间:2024/06/05 19:48

背景:

公司为节约成本,需要部署反向代理设备,用以缓存视频文件,节省机房出口带宽,增加廉价机房的上联出向流量。本人之前接触较多squid,但由于前公司使用时间较久,个人也是在使用上熟悉,接下来就是自己部署的过程记录,方便大家查阅。本文只解释编译安装的方法。

安装环境:

系统环境:
Ubuntu 12.04.1 LTS. x86_64   8G内存,12核心。由于是dell服务器,只得系统盘做了raid1,数据盘做了raid5.数据盘挂在了/srv下,整个磁盘大小是40T.可用大小37T。
软件环境:
登录到squid官网,http://www.squid-cache.org/Versions/  下载最新版本。因为只用到基本功能,因此,只需要下载稳定版本即可。这里我选择的是3.5.22版本,发布时间2016-10.9日,下载链接:http://www.squid-cache.org/Versions/v3/3.5/squid-3.5.22.tar.gz。

软件安装及配置:

下载上边软件链接到本地服务器上,解压后生成一个名为squid-3.5.22的文件夹,该文件便是squid的源码。详细使用方法可以参考这里,http://zyan.cc/book/squid/  此链接为squid的中文权威指南。对squid的基本使用和原理做了比较详尽的解释。
编译安装:
安装前先对ubuntu系统进行软件升级apt-get update安装编译需要的环境apt-get install build-essential下载安装wget http://www.squid-cache.org/Versions/v3/3.5/squid-3.5.22.tar.gztar -zxvf squid-3.5.22.tar.gzcd squid-3.5.22/./configure  --prefix=/var/www/dream/squid --sysconfdir=/var/www/dream/squid/etc--enable-arp-acl--enable-linux-netfilter--enable-pthreads--enable-err-language="Simplify_Chinese"--enable-default-err-language="Simplify_Chinese"--enable-storeio=ufs,aufs   ##确定使用存储的格式,这里由于我们存储较大视频文件10M左右甚至以上,所以这里选择aufs。--enable-baisc-auth-helpers="NCSA"--enable-underscoremakemake installln -s  /var/www/dream/squid/sbin/squid /usr/bin/squid  ##设置squid成为系统可以识别的系统命令。

配置:
#<common_begin############################## Common ACL Define #################################acl localhost src 127.0.0.1/255.255.255.255acl monitor src 127.0.0.1/255.255.255.255acl manager proto cache_objectacl SSL_ports port 443acl Safe_ports port 80          # httpacl Safe_ports port 21          # ftpacl Safe_ports port 443         # httpsacl Safe_ports port 70          # gopheracl Safe_ports port 210         # waisacl Safe_ports port 1025-65535  # unregistered portsacl Safe_ports port 280         # http-mgmtacl Safe_ports port 488         # gss-httpacl Safe_ports port 591         # filemakeracl Safe_ports port 777         # multiling httpacl CONNECT method CONNECThttp_access deny !Safe_portshttp_access deny CONNECT !SSL_portsacl PURGE method PURGEhttp_access allow PURGE localhosthttp_access deny purgehttp_access allow manager monitorhttp_access deny manager acl snmppublic snmp_community publicsnmp_access allow snmppublic localhostacl spider_ua req_header User-Agent -i spiderhttp_access deny spider_uaacl ip_in_fqdn url_regex -i ^http://[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/.*http_access deny ip_in_fqdn#acl allow_ip src "/var/www/dream/squid/etc/allow_ip"#http_access allow allow_ip#http_reply_access allow allow_ip######### Device Configuration Define ###############visible_hostname  proxy45-236#cache_dir Directory-Name Mbytes Level-1 Level2 cache_dir aufs /srv/cache1 3000000 128 128 min-size=1000000maximum_object_size 10240000 KBminimum_object_size 0 KBstore_dir_select_algorithm round-robincache_replacement_policy lrucache_swap_low 80cache_swap_high 85reload_into_ims on#request_header_access Via allow all#header_access Via allow all#header_access X-Cache deny all#header_access X-Cache-Lookup deny all#header_access X-Squid-Error deny alldns_timeout 2 minutedns_retransmit_interval 10 secondsnegative_dns_ttl 1 minute#refresh_stale_hit 0 minutevary_ignore_expire onrequest_timeout 40 secondspersistent_request_timeout 5 secondsconnect_timeout 1 minuteread_timeout 15 minutesserver_persistent_connections offclient_persistent_connections offcollapsed_forwarding onforwarded_for on#ignore_expect_100 onmaximum_object_size_in_memory 2 MBcache_mem 512 MBmemory_replacement_policy lru#logformat squid_custom_log %ts.%03tu %6tr %{X-Real-IP}>h %Ss/%03Hs %<st %rm %ru  %un %Sh/%<A %mt "%{Referer}>h" "%{User-Agent}>h" "%{Cookie}>h"logformat squid_custom_log %ts.%03tu %6tr %>a %Ss/%03Hs %<st %rm %ru  %un %Sh/%<A %mt "%{Referer}>h" "%{User-Agent}>h" "%{Cookie}>h"cache_access_log /var/www/dream/squid/var/logs/access.log squid_custom_logstrip_query_terms offlogfile_rotate 0cache_log /var/www/dream/squid/var/logs/cache.logcache_store_log none#http_port 80 accel vhost vport#http_port 9090 accel vhost vport=80 allow-direct http_port 80 accel vhost vport allow-direct #http_port 80icp_port 3130icp_access allow allsnmp_port 3401cache_effective_user squid cache_effective_group squidpid_filename /var/www/dream/squid/var/run/squid.piddiskd_program /var/www/dream/squid/libexec/diskdunlinkd_program /var/www/dream/squid/libexec/unlinkdhalf_closed_clients off#client_db off#cachemgr_passwd test4squid config#cache_mgr support@chinacache.comhosts_file /var/www/dream/squid/etc/hosts.conf#dns_nameservers 127.0.0.1dns_nameservers 8.8.8.8################Domain  Begin#####会把Range头去掉,再回后端服务器。所以都能取回整个文件。默认range_offset_limit 0 如果请求带有Range头,则squid会把Range头部转发到后端,不会预取多余的数据。range_offset_limit -1#这样的话比如一个1G的文件,用户下载到1M中断了,squid会一直把1G文件下载下来,但是如果文件大小超过"maximum_object_size"大小呢,下载完毕又放弃了。 quick_abort_min -1################Domain  End###### Add any of your own refresh_pattern entries above these.coredump_dir /var/www/dream/squid/var/cache/squidrefresh_pattern -i  ^http    1440   0%  1440   ignore-reload override-lastmodhttp_access allow allhttp_reply_access allow  all


以上配置可以让服务正常跑起来。


日志定期清理脚本并保留近30天日志

#!/bin/shset -eyesterday_date=$(date -d "1 day ago" +%Y%m%d)cd /var/www/dream/squid/log/# rename the current log file without interrupting the logging processmv access.log access.log.$yesterday_datemv cache.log cache.log.$yesterday_date# tell Squid to close the current logs and open new ones/var/www/dream/squid/sbin/squid -k rotate -f /var/www/dream/squid/etc/squid.conf/var/www/dream/squid/sbin/squid -k rotate -f /var/www/dream/squid/etc/squid1.conf/var/www/dream/squid/sbin/squid -k rotate -f /var/www/dream/squid/etc/squid2.conf/var/www/dream/squid/sbin/squid -k rotate -f /var/www/dream/squid/etc/squid3.conf# give Squid some time to finish writing swap.state filessleep 30mv access.log.$yesterday_date bak/mv cache.log.$yesterday_date bak/gzip -9 bak/access.log.$yesterday_dategzip -9 bak/cache.log.$yesterday_datefind bak/ -type f -ctime +30 | xargs rm



0 0
原创粉丝点击