Chinaunix首页 | 论坛 | 博客
  • 博客访问: 938566
  • 博文数量: 245
  • 博客积分: 11429
  • 博客等级: 上将
  • 技术积分: 2662
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-15 00:16
文章存档

2011年(56)

2010年(174)

2009年(15)

分类: LINUX

2010-09-08 22:45:59

squid5.3.0 安装 详解
作者:pkfeiyang  博客:http://blog.chinaunix.net/u3/103362/showart_2321852.html


软件

添加用户
useradd squid
groupadd squid
解压tar包
编译安装
./configure --prefix=/usr/local/squid3 \
--enable-debug-cbdata \
--enable-async-io=100 \
--with-pthreads \
--enable-storeio="aufs,diskd,ufs" \
--enable-removal-policies="heap,lru" \
--enable-icmp \
--enable-delay-pools \
--enable-useragent-log \
--enable-referer-log \
--enable-kill-parent-hack \
--enable-arp-acl \
--enable-default-err-language=Simplify_Chinese \
--enable-err-languages="Simplify_Chinese English" \
--disable-poll \
--disable-wccp \
--disable-wccpv2 \
--disable-ident-lookups \
--disable-internal-dns \
--enable-basic-auth-helpers="NCSA" \
--enable-stacktrace \
--with-large-files \
--disable-mempools \
--with-filedescriptors=65535 \
--enable-ssl \
--enable-x-accelerator-var \

--enable-linux-tproxy
  \
--enable-linux-netfilter \
--enable-poll (最好最好加上,不要和disable-poll同时使用)

make
make install


squid。conf 文件
squid.conf 添加如下:
#header_access Age deny all
#header_replace Age 1

//这样做的好处:
1.让client正常遵守源站的过期设置
2、可以很好的利用浏览器的缓存设置
3、可以减少很多304的请求
减少304请求的话 又有两个方面的好处 对于你们来讲的话 可以减少一定的带宽;
对于服务器端,减少了很多304的响应,可以一定程度提高机器性能


pid_filename /usr/local/squid3/var/logs/squid.pid

#accel listen port
http_port 3128
icp_port 3130
http_port 80 accel vhost vport

cache_peer 192.168.0.137 parent 80 0 no-query round-robin max-conn=32 originserver
#cache_peer_domain 这个不用加
 #做代理,直接写ip 就可以了  如果有时出现请求错误可以将max-conn的数值增大
只供参考

 #accel domain
cache_peer 192.168.0.137 parent 80 0 no-query originserver no-digest name=www
cache_peer_domain www .pkfeiyang.com .
pkfeiyang.com.cn .pkfeiyang.net .pkfeiyang.cn
cache_peer 192.168.0.130 parent 80 0 no-query originserver no-digest name=bbs
cache_peer_domain bbs .pkfeiyang.com .
pkfeiyang.com.cn .pkfeiyang.net .pkfeiyang.cn

#cache_peer bbs.pkfeiyang.com parent 80 0 no-query originserver name=bbs #定义不同的父节点,将节点设为no-query以及originserver说明这些节点是实际服务器

#cache_peer_domain    www
#cache_peer_domain    bbs bbs.pkfeiyang.com     #设定不同域名转发到不同的cache_peer上,如果没有这项.不同域名的域名可能被分发到同一台服务器上.



#acl all src 0.0.0.0/0.0.0.0
#http_access allow all    #允许所有客户端访问
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl to_localhost dst 127.0.0.0/8
acl SSL_ports port 443 563
acl Safe_ports port 80       # http
acl Safe_ports port 8080

acl LanDstDM dstdomain .
pkfeiyang.com .pkfeiyang.com.cn .pkfeiyang.net .pkfeiyang.cn
acl PURGE method PURGE
acl CONNECT method CONNECT
acl inside src 192.168.188.0/24
http_access allow inside
http_access allow PURGE localhost
#只有本机能删除了cache目标  squidclien -m PURGE -p 80 ***.com
http_access allow manager localhostcache_object是squid的缓存管理接口,这配置行只允许本机的缓存管理请求
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports

http_access allow LanDstDM
http_access deny   all
http_reply_access allow all


#base
visible_hostname bbs.cache.aa.com
cache_mgr pangkuo@kaa.com
cache_effective_user squid
cache_effective_group squid


error_directory /usr/local/squid3/share/errors/Simplify_Chinese
icon_directory /usr/local/squid3/share/icons
mime_table /usr/local/squid3/etc/mime.conf


cache_replacement_policy lru
#cache_dir
cache_dir aufs /data/cache1 32768 64 64
cache_dir aufs /data/cache2 32768 64 64
coredump_dir /var/spool/squid (squid挂掉后,临终遗言要放到哪里。不用管,一般人看不懂)
cache_mem 2048 MB
max_open_disk_fds 0
#maximum_object_size 512 KB
#maximum_object_size_in_memory 256 KB
maximum_object_size 20 MB
maximum_object_size_in_memory 8 MB


#keepalived
client_persistent_connections off
server_persistent_connections on
#persistent_request_timeout 60 seconds

#memory_pools on
#memory_pools_limit 64 MB

forwarded_for on
log_icp_queries off


via off
httpd_suppress_version_string off


ie_refresh off
tcp_recv_bufsize 32 KB





ipcache_size 1024
ipcache_low 90
ipcache_high 95
fqdncache_size 1024

memory_replacement_policy lru

hosts_file /etc/hosts
request_header_max_size 128 KB


#deny cache
hierarchy_stoplist cgi-bin ? \.php
acl QUERY urlpath_regex cgi-bin\? \.php
acl DIRECT url_regex -i ^http:\/\/bbs\.aa.com\.com\/$
acl DIRECT url_regex -i ^http:\/\/photo\.aa\.com\/.*$
acl DIRECT url_regex -i ^http:\/\/www\.aa\.com\/index\.html$
cache deny QUERY
cache deny DIRECT


#request_body_max_size 0 KB


refresh_pattern ^ftp:           60      20%     10080
refresh_pattern ^gopher:        60      0%      1440
refresh_pattern .               0       20%     1440
refresh_pattern -i \.css$       360    50%     2880    reload-into-ims
refresh_pattern -i \.js$        1440    50%     2880    reload-into-ims
refresh_pattern -i \.html$        720   50%     1440    reload-into-ims
refresh_pattern -i \.jpg$       1440    90%     2880    ignore-reload
refresh_pattern -i \.gif$       1440    90%     2880    ignore-reload
refresh_pattern -i \.swf$       1440    90%     2880    ignore-reload
refresh_pattern -i \.jpg$       1440      50%     2880    ignore-reload
refresh_pattern -i \.png$       1440      50%     2880      ignore-reload
refresh_pattern -i \.bmp$       1440      50%     2880      ignore-reload




quick_abort_min 20 KB
quick_abort_max 20 KB
quick_abort_pct 95

connect_timeout 1 minute
negative_ttl 0 minutes
read_timeout 30 seconds
pconn_timeout 120 seconds
shutdown_lifetime 5 seconds
strip_query_terms off



icp_port 0

# logfile
emulate_httpd_log on
#logformat combined %{X-Forwarded-For}>h %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %h" "%{User-Agent}>h" %Ss:%Sh
#access_log none
#access_log /data/logs/access.log combined
logformat squid  %ts.%03tu %6tr %>a %Ss/%03Hs %access_log /var/squid/log/logs/access.log squid
#cache_store_log /var/squid/store.log
cache_store_log /dev/null
cache_log /var/squid/log/logs/cache.log
logfile_rotate 12


# MISCELLANEOUS
store_objects_per_bucket 15
client_db off
 


chown -R squid:squid /data/  #cache有squid权限
b.对cache目录进行初始化

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

2004/11/01 23:06:29| Creating Swap Directories

FATAL: Failed to make swap directory /Cache1/00: (13) Permission denied

Squid Cache (Version 2.5.STABLE7): Terminated abnormally.

CPU Usage: 0.000 seconds = 0.000 user + 0.000 sys

Maximum Resident Size: 0 KB

Page faults with physical i/o: 10


如果出现上面这样的错误信息,表示你/Cache1目录权限错误,请检查/Cache1目录所有者是否为squid用户所有。

c.启动squid

#su squid -c "/usr/local/squid/bin/RunCache &"



d.关闭squid

#/usr/local/squid/sbin/squid -k shutdown

要执行两次才能正常关闭suqid
3.设置iptables支持透明代理
设置squid+iptables支持透明代理前请先设置好NAT,可以使用下面的简单语句

echo "1" >; /proc/sys/net/ipv4/ip_forward   #设置转发
 /sbin/iptables -t nat -A POSTROUTING -j MASQUERADE 
  iptables -t nat -A PREROUTING -i eth1 -p tcp -s 192.168.188.0/24 --dport 80 -j REDIRECT --to-ports 3128

e.重新读取squid.conf文件

#/usr/local/squid/sbin/squid -k reconfigure

需要执行两次才能重新读取squid.conf文件
具体文件修改参考:squid-cache.org/Versions/v3/3.1/cfgman/

    想要启用squid所需的改变想要更好的利用squid的cache功能,不是把它启用了就可以的,我们需要做以下几个调整:
1、启用apache的 mod_expires 模块,修改 httpd.conf,加入以下内容:
#expiresdefault "modification plus 2 weeks"expiresactive
onexpiresbytype text/html "access plus 10 minutes"expiresbytype
image/gif "modification plus 1 month"expiresbytype image/jpeg "modification
plus 1 month"expiresbytype image/png "modification plus 1
month"expiresbytype text/css "access plus 1 day"expiresbytype
application/x-shockwave-flash "access plus 3 day"
以上配置的作用是规定各种类型文件的cache规则,对那些图片/flash等静态文件总是cache起来,可根据各自的需要做适当调整。
第二种:
  /usr/local/111/bin/apxs -i -c /home/software/bak/httpd-2.2.3/modules/metadata/mod_expires.c
/usr/local/apache111/bin/apxs -i -c /home/software/bak/httpd-2.2.3/modules/metadata/mod_headers.c
vi /usr/local/apache111/bin/apr-1-config
改 LDFLAGS="" 为 LDFLAGS="-lz"
/usr/local/apache111/bin/apxs -i -c /home/software/bak/httpd-2.2.3/modules/filters/mod_deflate.c
httpd.conf配置:
LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so
LoadModule expires_module modules/mod_expires.so
   第一种:

ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/png A2592000
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A604800
ExpiresByType text/css A604800



SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css image/gif image/jpeg image/png application/x-javascript

2、修改 php.ini 配置,如下:
session.cache_limiter = nocache
以上配置的作用是默认取消php中的cache功能,避免不正常的cache产生。
3、修改应用程序例如,有一个php程序页面static.php,它存放着某些查询数据库后的结果,并且数据更新并不频繁,于是,我们就可以考虑对其cache。只需在static.php中加入类似如下代码:
header('Cache-Control: max-age=86400
,must-revalidate');header('Pragma:');header('Last-Modified: ' .
gmdate('D, d M Y H:i:s') . ' GMT' );header("Expires: " .gmdate ('D, d M Y
H:i:s', time() + '86400' ). ' GMT');
以上代码的意思是,输出一个http头部信息,让squid知道本页面默认缓存时长为一天


防盗链技术现状:

1、通过识别Referer确认请求来源页面
2、Apache,squid等都能对Referer进行识别
3、通过ActiveX显示的内容不向服务器提供Referer Header(例如,Flash,WindowsMedia视频
等)
4、流媒体的RTSP协议也不向服务器提供Referer Header
5、通过服务器端程序代码实现
防盗链应用现状:
1、对图片、HTML等可以实现防盗链
2、无法对Flash,WindowsMedia视频
(MMS,RTSP)实现防盗链
3、服务器端程序代码实现的防盗链无法通过CDN加速
对于Flash,WindowsMedia视频
这种占用流量较大的服务无法实现防盗链,对一个依靠这类内容作为盈利点的网站来说是非常头疼的,俺通过一些研究以及测试实现了采用Cookie技术的防盗链解决方案,完美的解决了对Flash,WindowsMedia视频
的防盗链。
首先发现虽然ActiveX插件不传递Referer,但是却忠实的传递Cookie。于是在显示ActiveX的页面的 标签内嵌入一段代码:

这段代码用 javascript 设置了一段 Cookie: Cache=vod
然后通过各种ACL来判断这个Cookie的存在以及验证其值的操作了
Squid:
建立脚本 /usr/local/squid/libexec/squid_cookie.pl
????
#!/usr/bin/perl -w
# 这个脚本仅仅是验证了Cache这个cookie的存在,没有严格的校验其值。
# This is the cookie to check for.
$COOKIE=”Cache=”;
# disable output buffering
$|=1;
# cookie matches?
while () {
chop;
$cookie=$_;
if( $cookie =~ /$COOKIE/i) {
    print “OK\n”;
} else { print “ERR\n”; }
}
????
然后在squid.conf添加:
external_acl_type download children=15 %{Cookie} /usr/local/squid/libexec/squid_cookie.pl
acl dl external download
然后选择需要进行防盗链的文件类型:
acl filetype url_regex -i \.wmv
acl filetype url_regex -i \.wma
acl filetype url_regex -i \.asf
acl filetype url_regex -i \.asx
acl filetype url_regex -i \.avi
acl filetype url_regex -i \.mp3
acl filetype url_regex -i \.smi
acl filetype url_regex -i \.rm
acl filetype url_regex -i \.ram
acl filetype url_regex -i \.rmvb
acl filetype url_regex -i \.swf
acl filetype url_regex -i \.mpg
acl filetype url_regex -i \.mpeg
acl filetype url_regex -i \.mov
acl filetype url_regex -i \.zip
acl filetype url_regex -i \.mid
如果仅仅只是禁止用户访问的话,就没意思了,要让盗链者帮我们宣传我们的网站,特别是发现盗链比较多的时候,这个时候,可以让任何盗链的网站帮我们免费宣传~~~那就是把盗链的url重定向到我们的网站宣传页~~
建立脚本:/usr/local/squid/libexec/squid_redir.pl
?????????
#!/usr/bin/perl -T -w
#
# rredir.pl
#
# Description: Direct all request to files who are in a local dir to
# this directory
#
use File::Basename;
use URI::URL;
# flush after every print
$| = 1;
# Process lines of the form ‘URL ip-address/fqdn ident method’
# See release n****s of Squid 1.1 for details
while ( <> ) {
$r302=0;
($url, $addr, $fqdn, $ident, $method) = m\S*) (\S*)/(\S*) (\S*) (\S*):;
$url = url $url;
$host = lc($url->host);
if ( $host !~ /\./ ) {
   next;
}
if ( $host =~ /vod\.domain\.com/ ) {
   $url->path(”/ad.wmv”);
   $r302=1;
}
} continue {
if ( $r302 ) {
   print “302url\n”;
} else {
   print “$url $addr/$fqdn $ident $method\n”;
}
}
?????????
然后在squid.conf添加:
redirect_program /usr/local/squid/libexec/squid_redir.pl
redirect_children 5
acl superurl url_regex -i ^\.domain\.com/tom\.wmv$
redirector_access deny superurl
redirector_access allow filetype !dl
redirector_access deny all
设置superurl是因为宣传我们自己站点的视频
是不做防盗链的,这样才能起到宣传的作用。现在大功告成啦!网站的流量大幅增加~~~PV是原来的三倍,Oh,Yeah~
WMS视频
的MMS协议由于不是明文,无法实现防盗链,但是RTSP协议基本就是HTTP协议的变种,可以在BIGIP等可以进行Layer 7处理数据的设备上实现对Cookie的校验。但是WMS视频
防盗链要禁止MMS协议,因为MMS协议不是明文,无法进行cookies的识别。但是可以在HTTP,RTSP上进行识别,WMS V9都是支持HTTP,RTSP的。下面是用于BIGIP的防盗链iRule:
?????????
if (http_uri matches_regex “vod.domain.com”) {
if (http_cookie(”Cache”) == “vod”) {
   use pool vod-rtsp
}
else {
   discard
}
}
else {
use pool vod-rtsp
}
?????????
其他的系统可以参考以上的规则。
采用Cookie的防盗链方式对于大多数的系统都可以轻易实现,Cookie一般都是用来传递认证信息,相信不会出现不传递的状况。参考

Squid安装调试中的几个常用命令

1,初始化你在 squid.conf 里配置的 cache 目录
#squid/sbin/squid -z    //
第一次启动squid服务时必须输入此命令
如果有错误提示,请检查你的 cache目录的权限。
2
,对你的squid.conf 排错,即验证 squid.conf 语法和配置。
#squid/sbin/squid -k parse
如果squid.conf 有语法或配置错误,这里会返回提示你,如果没有返回,恭喜,可以尝试启动squid
3
,在前台启动squid,并输出启动过程。
#squid/sbin/squid -N -d1
如果有到 ready to server reques,恭喜,启动成功。
然后 ctrl + c,停止squid,并以后台运行的方式启动它。
4
,启动squid在后台运行。
#squid/sbin/squid -s
这时候可以 ps -A 来查看系统进程,可以看到俩个 squid 进程。
5
,停止 squid
#squid/sbin/squid -k shutdown
这个不用解释吧。
6
,重引导修改过的 squid.conf
#squid/sbin/squid -k reconfigure
这个估计用的时候比较多,当你发现你的配置有不尽你意的时候,可以随时修改squid.conf,然后别忘记对你的 squid.conf排错,然后再执行此指令,即可让运行中squid重新按照你的squid.conf 来运行。
7
,把squid添加到系统启动项
编辑 /etc/rc.d/rc.local
添加如下行:   /usr/local/squid/sbin/squid -s
当然,并不是每个人都喜欢这种启动方式,你可以用你最习惯的方式;或者把它安装为服务。

 

参考:



错误收集;

ERROR

The requested URL could not be retrieved


当尝试取回该 URL 时遇到下面的错误:

访问被拒绝。

Access control configuration prevents your request from being allowed at this time. Please contact your service provider if you feel this is incorrect.

缓存服务器的管理员 .

解决一:


这是由于代理服务器机器的DNS配置问题,代理服务器机器不能找到指定urlip地址导致。检查/etc/resolv.conf文件配置是否正确,如不正确添加正确的配置信息,比如说:
nameserver ###.###.###.###
nameserver ###.###.###.###
可以配置多个DNS server
重新启动squid服务。

解决方法二:

访问控制http_access allow   all

 

当尝试取回该 URL 时遇到下面的错误:

目前无法将您的请求进行转送操作

This request could not be forwarded to the origin server or to any parent caches. The most likely cause for this error is that the cache administrator does not allow this cache to make direct connections to origin servers, and all configured parent caches are currently unreachable.

参考:

 

 

 

cache_peer 192.168.0.137       parent    80  0 no-query round-robin max-conn=32 originserver
squid2.5
以上都是用的cache_peer来指定所需要代理的服务器的IP 这一点很重要!

 

 

 

WARNING: Netmasks are deprecated. Please use CIDR masks instead.

2010/10/31 21:39:41| WARNING: IPv4 netmasks are particularly nasty when used to compare IPv6 to IPv4 ranges.

2010/10/31 21:39:41| WARNING: For now we will assume you meant to write /28

2010/10/31 21:39:41| WARNING: Netmasks are deprecated. Please use CIDR masks instead.

2010/10/31 21:39:41| WARNING: IPv4 netmasks are particularly nasty when used to compare IPv6 to IPv4 ranges.

2010/10/31 21:39:41| WARNING: For now we will assume you meant to write /28

[root@monitor html]# vi /usr/local/squid3/etc/squid.conf

 

J解决:192.168.188.0/24   改成这个形式就可以了




阅读(4562) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~