Chinaunix首页 | 论坛 | 博客
  • 博客访问: 327865
  • 博文数量: 103
  • 博客积分: 3886
  • 博客等级: 中校
  • 技术积分: 1005
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-28 15:00
文章分类

全部博文(103)

文章存档

2011年(30)

2010年(73)

分类: LINUX

2010-08-10 11:11:13

1.1. 安装Squid

Squid,有趣的鱿鱼代理服务器

1.1.1. squid2.5安装

1.1.1.1. 下载&安装

增加文件描述符,

vi /etc/security/limits.conf

在未段加下两行

* soft nofile 10240

* hard nofile 10240

##############################################结束

[root@xok.cn squid]ulimit -HSn 10240

#增加临时端口范围

加下列行到/etc/rc.local

echo "1024 40000" > /proc/sys/net/ipv4/ip_local_port_range

重启服务器

groupadd -g5300 squid

useradd -u 5300 -g 5300 -s /sbin/nologin squid

export CFLAGES='-O2 -mcpu=pentium4 -march=pentium4 -mmmx -msse -msse2'

(可以根据你的CPU选择相应的参数

GCC-3.1以上可針對CPU最佳化:

Pentium2: -O2 -mcpu=i686 -march=i686 -mmmx

Pentium3: -O2 -mcpu=pentium3 -march=pentium3 -mmmx -msse

Pentium4: -O2 -mcpu=pentium4 -march=pentium4 -mmmx -msse -mss)

squid的2.5最新版本是squid-2.5.STABLE6,先下载安装包,再安装:

#cd /tmp

#mkdir squidinstall

#cd squidinstall

#wget

#tar xzvf squid-2.5.STABLE6.tar.gz

#cd squid-2.5.STABLE6

#./configure –prefix=/usr/local/squid –enable-async-io=100 –enable-storeio=aufs,coss,diskd,ufs –enable-icmp –enable-kill-parent-hack –enable-cache-digests –enable-default-err-language=Simplify_Chinese enable-err-language="Simplify_Chinese English" –enable-poll –enable-removal-policies="lru,heap" –disable-ident-lookups –disable-internal-dns –disable-hostname-checks –enable-truncate –enable-underscores –enable-stacktrace –disable-arp-acl

#make

#make install

mkdir /cache

chown -R squid:squid /cache

chown squid:squid -R /usr/local/squid

chown -R squid:squid /usr/local/squid/var

看到类似于下图的提示,并且没有出现 Error Code :1 之类的错误提示,证明Squid已经安装完成了!

2.1.

配置文件squid.conf

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

找到:

cache_dir ufs

修改列:

cache_dir aufs /cache 1800 16 256

找到:

# http_port 3128

修改列:

http_port 8080 (去掉注释)

找到下列行:

acl QUERY urlpath_regex cgi-bin ?

no_cache deny QUERY

改为:

acl QUERY urlpath_regex -i cgi-bin ? .php$ .asp$ /$ ^$

no_cache deny QUERY

acl nodown urlpath_regex -i .mp3$ .avi$ .wma$ .exe$ .rar$ .zip$

no_cache deny nodown

#禁止cgi-bin,php,asp,mp3,avi,zip等缓存

找到:

httpd_accel_with_proxy off

修改列:httpd_accel_with_proxy on

#这个很重要,疏忽了它我找了大半天为什么…..

找到:

# cache_effective_user nobody

修改列:

cache_effective_user squid

cache_effective_group squid

# 以下挨着去找并修改:

cache_mem 300 MB(改为约实际内存1/3)

maximum_object_size 2048 KB

maximum_object_size_in_memory 800 KB

cache_store_log none

emulate_httpd_log on

dns_children 20

hosts_file /usr/local/squid/etc/hosts

redirect_children 20

refresh_pattern . 0 50% 60

在行# And finally deny all other access to this proxy后改

http_access allow all

httpd_accel_host virtual

httpd_accel_uses_host_header on

logfile_rotate 24

log_icp_queries off

buffered_logs on

coredump_dir /cache

ie_refresh on

好了,运行你的Squid吧!

#/usr/local/squid/sbin/squid start

Q:如何判断已经成功实现缓存功能

A:

#ps -waux | grep squid

#cat /var/log/squid/cache.log (有没有正常输出)

#netstat -a | more (找3128端口)

在IE里代理设置好缓存服务器的地址端口.打一个URL,如:

blog.xok.cn

正常显示,就成功啦!!!

如果乱打blogaaa.xok.cn看到:

您所请求的网址(URL)无法获取

——————————————————————————–

当尝试读取以下网址(URL)时: http://blogaaa.xok.cn/

发生了下列的错误:

无法将您输入的主机名称:blogaaa.xok.cn转换成 IP 地址 域名服务器返回以下讯息:

Name Error: The domain name does not exist. 这表示:

The cache was not able to resolve the hostname presented in the URL. Check if the address is correct. 缓存服务器无法解析您输入网址(URL)中的主机名称,请检查该名称是否正确。

本缓存服务器管理员:xxx@xok.cn

——————————————————————————–

Generated Sun, 11 Jul 2004 06:00:58 GMT by blog.xok.cn(squid/2.5.STABLE5)

3.1.

浅谈Squid的ACL语法.

ACL,Access Control List,访问控制列表.它的语法是: (在/usr/local/squid/etc/squid.conf里添加)

acl 表名 表类型 [-i] 表的值

http_access [allow/deny] 表名下面分条解释:

表名:可以自定义

表类型:表类型有

src 源地址:客户机的IP地址

dst 目的地址:服务器的IP地址

srcdomain 源域:客户机所属的域

dstdomain 目的域:服务器所属的域

url_regex URL正则表达式(字符串部分)

urlpath_regex URL正则表达式中的路径

time [星期] [时间段]

maxconn 客户端的最大连接数

-i 这个参数使Squid不区分大小写

表的值:随表的类型不同而不同

注意:time中的星期要用如下字符:

S (Sunday,星期日) M(Monday,星期一) T(Tuesday,星期二) W(Wednesday,星期三)

H(Thursday,星期四) F(Friday,星期五) A(Saturday,星期六)

时间段的表示方式是: XX:00-YY:00 如: 20:00-22:00

http_access 选项允许你设置一个表是允许(allow)还是拒绝(deny)

下面举几个例子: 防3721的ACL.在squid.conf中加入:

acl badurls dstdomain -i download.3721.com cnsmin.3721.com

http_access deny badurls

acl badkeywords url_regex -i 3721.com 3721.net

http_access deny badkeywords

解释:

badurls 和 badkeywords 是你自定义的表名.

dstdomain 是服务器的域名(目的域) 而 url_regex 则是URL正则表达式(字符串部分)包含的内容.

http_access 选项的 deny 则是把表badurls和表badkeywords的访问拒绝.

禁止下载Flash:

acl badfiles urlpath_regex -i .swf$

http_access deny badfiles

大家在今后的配置中,慢慢体会ACL的用法吧! 达到目的喽:

错误

您所请求的网址(URL)无法获取

——————————————————————————–

当尝试读取以下网址(URL)时: 发生了下列的错误:

Access Denied. 拒绝访问

Access control configuration prevents your request from being allowed at this time. Please contact your service provider if you feel this is incorrect. 当前的存取控制设定禁止您的请求被接受,如果您觉得这是错误的,请与您网路服务的提供者联系。



squid.conf实例

http_port 127.0.0.1:80 vhost vport

cache_mem 3072 MB
cache_swap_high 95
cache_swap_low 80
cache_dir ufs /data/cache 4096 64 256
maximum_object_size 10 MB
maximum_object_size_in_memory 6 MB
half_closed_clients off
cache_store_log none
emulate_httpd_log on
logformat mobile "%>a"%{%Y-%m-%d %H:%M:%S}tl"%rm %ru HTTP/%rv"%Hs"%h"%{User-Agent}>h"%Ss:%Sh"
pid_filename /log/squid/squid.pid
access_log /log/squid/access.log mobile
cache_log /log/squid/cache.log

logfile_rotate 31

acl tianya referer_regex -i tianya
http_access deny tianya
deny_info tianya

acl AntiBaidu req_header User-Agent Baiduspider
http_access deny AntiBaidu

acl Manager proto cache_object
acl Localhost src 127.0.0.1 10.175.114.81
http_access allow Manager Localhost
http_access deny Manager

acl Safe_ports port 80 # http
acl all src 0.0.0.0/0.0.0.0
http_access deny !Safe_ports
http_access allow all

visible_hostname cache.zqgame.com
cache_mgr webmaster@zqgame.com

cache_effective_user squid
cache_effective_group squid
tcp_recv_bufsize 65535 bytes


cache_peer 121.10.243.193 parent 80 0 no-query originserver name=s16
cache_peer 121.10.245.73 parent 80 0 no-query originserver name=s18
cache_peer 121.10.245.81 parent 80 0 no-query originserver name=s19
cache_peer 218.16.224.204 parent 80 0 no-query originserver name=bw
cache_peer_domain s16 s16.dh.ucjoy.com
cache_peer_domain s18 s18.dh.ucjoy.com
cache_peer_domain s19 s19.dh.ucjoy.com
cache_peer_domain bw gameupdate1.bw.zqgame.com

error_directory /opt/squid/share/errors/Simplify_Chinese

icp_port 0

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