ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Freebsd 下安装squid

Freebsd 下安装squid

原创 Linux操作系统 作者:lizhaohua1998 时间:2010-12-10 11:33:10 0 删除 编辑
1、获取Squid源代码
安裝 wget
cd /usr/ports/ftp/wget
make install clean
下载最新squid上传到服务器上或者
cd /home/zhangshi
wget http://www.squid-cache.org/Versions/v3/3.1/squid-3.1.9.tar.gz
2、编译及安装
解压缩
tar -zxvf squid-3.1.9.tar.gz
cd squid-3.1.9
配置
./configure --prefix=/usr/local/squid --enable-arp-acl --enable-linux-netfilter --enable-err-language="Simplify_Chinese" --enable-default-err-language="Simplify_Chinese" --enable-auth="basic" --enable-baisc-auth-helpers="NCSA" --enable-underscore --enable-delay-pools

--prefix=/usr/local/squid #指定squid安装目录
--enable-arp-acl #客户端的MAC地址进行管理
--enable-linux-netfilter #允许使用Linux的透明功能,目前3.0以上好像不支持此功能
#--enable-pthreads
--enable-err-language="Simplify_Chinese" # 错误报告消息语言,繁体为Traditional_Chinese
--enable-default-err-language="Simplify_Chinese" #设置error_directory默认中文,繁体为Traditional_Chinese
--enable-auth="basic" #认证方式
--enable-baisc-auth-helpers="NCSA" #认证程序
--enable-underscore #允许解析的URL中出现下划线
--enable-delay-pools #限制带宽

其他如:
./configure
--prefix=/usr/local/squid #指定squid安装目录
--mandir=/usr/share/man #指定man的安装目录
--enable-async-io=20 #采用同步io提高性能,负载高可设大点
--disable-icmp #禁止icmp协议代理(默认不允许)
--enable-delay-pools #允许延迟限止带宽
--enable-cache-digests #允许缓存摘要,可以加快请求缓冲内容的速度(集群用)
--enable-snmp #允许snmp协议支持(如用mrtg进行流量监控等)
--disable-ident-lookups #禁止使用RFC931识别
--enable-epoll #支持epoll的IO模式,2.6以上内核才具有
--enable-basic-auth-helpers="NCSA" #允许带密码验证,NCSA 风格的用户名和密码档
--enable-linux-netfilter #允许透明代理
--enable-arp-acl #允许基于MAC地址的存取过滤
--enable-err-language="Simplify_Chinese" #支持的错误语言
--enable-default-err-language="Simplify_Chinese" #指定默认的错误语言
error_directory /usr/local/etc/squid/errors/Traditional_Chinese



编译
make
安装
make install
3、编译生成Squid认证程序ncsa_auth
cd helpers/basic_auth/NCSA/
make
cp ncsa_auth /usr/sbin/
4、使用htpasswd来生成用户名/密码对应的文件
cd /usr/local/squid
htpasswd -c /usr/local/squid/password zhangshi
在输入两边密码后,一个zhangshi用户就生成了。如果以后需要添加用户,把上面的命令去掉-c参数再运行即可。
第一次用c,以后不要加-c
5、修改Squid配置文件
mv -f etc/squid.conf etc/squid.conf.bak
ee etc/squid.conf
插入如下内容:

其中password 文件使用apache的 htpasswd命令生成

下面是设置好的squid.conf的全部





http_port 3128
#icp_port 3130
cache_mgr zs895@163.com
cache_effective_user squid
cache_effective_group squid
visible_hostname proxy
cache_dir ufs /tmp 1000 64 1024
cache_mem 172 MB
hierarchy_stoplist cgi-bin ?
hierarchy_stoplist -i ^https: ?
acl QUERY urlpath_regex -i cgi-bin ? .asp .php .jsp .cgi
#acl denyssl urlpath_regex -i ^https:
no_cache deny QUERY
#no_cache deny denyssl
cache_swap_low 85
cache_swap_high 90
coredump_dir /usr/local/squid/var/logs
cache_access_log /usr/local/squid/var/logs/access.log
cache_store_log /usr/local/squid/var/logs/store.log
cache_log /usr/local/squid/var/logs/cache.log
dns_nameservers 61.177.7.1 221.228.255.1
auth_param basic program /usr/sbin/ncsa_auth /usr/local/squid/password
auth_param basic children 32
#auth_param basic credentialsttl 2 hours
authenticate_ttl 24 hour
auth_param basic realm Please enter the user name and password

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
#acl all src 0.0.0.0/0.0.0.0


#acl localhost src 192.168.2.0/24 192.168.1.0/24
acl manager proto cache_object
acl localhost src 127.0.0.1/32
http_access allow manager localhost
http_access deny manager

#acl our_networks src 192.168.138.0/24 192.168.168.0/24
#http_access deny !our_networks

#acl alloweddomain url_regex -i liufeng.com.cn
#http_access allow alloweddomain

#acl alloweddomain1 url_regex -i squid-cache.org
#http_access allow alloweddomain1

#acl badsrvip2 dst 1.2.3.4
#http_access allow badsrvip2

#acl alloweddomains url_regex "/usr/local/squid/etc/acl/alloweddomain.txt"
#http_access allow alloweddomains


#acl advance arp 00:90:90:5c:0f:95 00:90:90:5c:0f:97
#http_access allow advance

#acl localmac arp "/usr/local/squid/etc/acl/localmac.txt"
#http_access allow localmac

#acl office src "/usr/local/squid/etc/acl/worklist.txt"
#http_access allow office

#acl advanceip1 src 192.168.168.100
#http_access allow advanceip1

#acl badclientip1 src 192.168.168.200
#http_access deny badclientip1

#acl badclientnet1 src 192.169.168.0/24
#http_access deny badclientnet1

#acl badsrvip1 dst 1.2.3.4
#http_access deny badsrvip1

#acl baddomain1 dstdomain -i
#http_access deny baddomain1

#acl badurl1 url_regex -i qq.com
#http_access deny badurl1

#acl allow_list url_regex "/usr/local/squid/etc/acl/allow.list.txt"
#http_access allow allow_list

#acl deny_list url_regex "/usr/local/squid/etc/acl/deny.list.txt"
#http_access deny deny_list

#acl badhead dstdom_regex -i ^game ^pic ^xxx ^sex
#http_access deny badhead

#acl badurl2 url_regex -i game pic sex mp3 xxx
#http_access deny badurl2

#acl client1 src 192.168.168.50
#acl conn1 maxconn 5
#http_access deny client1 conn1

#S - Sunday, M - Monday, T - Tuesday, W - Wednesday, H - Thursday, F - Friday A - Saturday
#acl clientnet2 src 192.168.142.0/24
#acl worktime time MTWHF 12:00-13:00
#http_access deny clientnet2 worktime

#acl badfile1 urlpath_regex -i .mp3$ .exe$ .zip$ .rar$ .torrent$ .avi$
#http_access deny badfile1

#acl qq url_regex -i tencent.com
#http_access deny qq

acl normal proxy_auth REQUIRED
http_access allow normal

http_access deny !Safe_ports

http_access deny CONNECT !SSL_ports

http_access deny all
icp_access allow all

# Add any of your own refresh_pattern entries above these.
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|?) 0 0% 0
refresh_pattern . 0 20% 4320

error_directory /usr/local/squid/share/errors/zh-cn

pid_filename /var/run/squid.pid

#request_body_max_size 20 KB



中文解释:

####################################
#基本信息:
#定义代理服务器端口
http_port 192.168.168.40:3128
icp_port 3130
#定义管理员邮件地址
cache_mgr zs895@163.com
#设置缓存的有效用户
cache_effective_user squid
#设置缓存的有效组
cache_effective_group squid
#运行squid的主机的名字,可以写上该机器的IP地址
visible_hostname proxy


####################################
#缓存选项
#定义硬盘缓存空间,其中1000代表缓存空间最大为1000M;16和256代表缓存目录下的一级和二级目录数
cache_dir ufs /tmp 1000 64 1024
# cache_dir null /tmp 表示不需要缓存策略
#定义缓存内存,一般定义为物理内存大小的1/3。默认设置为8MB
cache_mem 172 MB
#用来强制某些特定的对象不被缓存,主要是处于安全的目的。
hierarchy_stoplist cgi-bin ?
hierarchy_stoplist -i ^https: ?
#acl QUERY urlpath_regex -i cgi-bin ? .asp .php .jsp .cgi
#acl denyssl urlpath_regex -i ^https:
#no_cache deny QUERY
#no_cache deny denyssl
#指定进行空间交换的空占比(缓存数值超过或低于某个百分比时和交换空间进行数据交换)
cache_swap_low 85
cache_swap_high 90
#指最大的能保存在内存中的对象。如果内存足够多,可以适当将数值调整得大一些,可以加快客户机读取大对象的速度。
maximum_object_size 8 KB
#大于该值的对象将不被存储在缓存里。如果要提高访问速度,就降低该值;如果想最大限度节约带宽,降低成本,就增加该值。建议将##它的值改为1024 KB。
maximum_object_size 4096 KB

####################################
#日志选项
#定义dump的目录,当squid突然挂掉的时候,或突然出现什么故障的时候,将squid在内存中的资料写到硬盘中
coredump_dir /usr/local/squid/var/logs

#设置访问日志文件
cache_access_log /usr/local/squid/var/logs/access.log
#设置缓存日志文件
cache_log /usr/local/squid/var/logs/cache.log
#设置网页存储日志
cache_store_log /usr/local/squid/var/logs/store.log



####################################
#Dns服务器
dns_nameservers 61.177.7.1 221.228.255.1



####################################
#身份验证:
#选项定义了认证方式为basic,认证程序的路径和认证需要读取的帐户文件
auth_param basic program /usr/sbin/ncsa_auth /usr/local/squid/password

#此选项定义了认证程序的进程为5
auth_param basic children 5

#此选项定义了认证程序的时间为2小时,如果2小时过后用户还需要使用squid,则必须重新输入帐户和密码
auth_param basic credentialsttl 2 hours

#选项定义了用户的登录时显示的提示内容
auth_param basic realm Please enter the user name and password

#端口设定
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

#本机可以上网
acl localhost src 127.0.0.1/32
http_access allow localhost

#本网段用户可以上网,其余网段禁止
acl our_networks src 192.168.168.0/24 192.168.142.0/24 192.168.169.0/24
http_access deny !our_networks

#所有用户都可以访问以下网站
acl alloweddomain url_regex -i liufeng.com.cn
http_access allow alloweddomain

#所有用户都可以访问以下ip
acl badsrvip2 dst 1.2.3.4
http_access allow badsrvip2

#所有用户都可以访问文件中的网站,alloweddomain.txt里是允许访问站点的列表,每行一个。
#acl alloweddomains url_regex "/usr/local/squid/etc/acl/alloweddomain.txt"
#http_access allow alloweddomains

#本网卡mac地址用户可以直接上网(高级用户)
acl advance arp 00:90:90:5c:0f:95 00:90:90:5c:0f:97
http_access allow advance

#mac地址文档中的用户可以直接上网,启用则去掉注释符
#acl localmac arp "/usr/local/squid/etc/acl/localmac.txt"
#http_access allow localmac

#ip地址文档中的用户可以直接上网,启用则去掉注释符
#acl office src "/usr/local/squid/etc/acl/worklist.txt"
#http_access allow office

#本网卡ip用户可以直接上网(高级用户)
acl advanceip1 src 192.168.168.100
http_access allow advanceip1

#禁止IP地址为192.168.168.200的客户机上网。
acl badclientip1 src 192.168.168.200
http_access deny badclientip1

#禁止192.168.169.0这个子网里所有的客户机上网。
acl badclientnet1 src 192.169.169.0/24
http_access deny badclientnet1

#禁止用户访问IP地址为1.2.3.4的网站。
acl badsrvip1 dst 1.2.3.4
http_access deny badsrvip1

#禁止用户访问域名为的网站,-i表示不区分大小写
#acl baddomain1 dstdomain -i
http_access deny baddomain1

#禁止用户访问域名包含有qq.com的网站。
acl badurl1 url_regex -i qq.com
http_access deny badurl1

# 定义allow.list和deny.list路径,禁止或者允许上网的网站地址
# 允许allow.list 的域名列表被访问
#acl allow_list url_regex "/usr/local/squid/etc/acl/allow.list.txt"
#http_access allow allow_list

# 拒绝deny.list 的域名列表被访问
#acl deny_list url_regex "/usr/local/squid/etc/acl/deny.list.txt"
#http_access deny deny_list


#禁止某些关键字开头的网址
acl badhead dstdom_regex -i ^game ^pic ^xxx ^sex
http_access deny badhead

#禁止用户访问域名包含有sex等关键字的URL。
acl badurl2 url_regex -i game pic sex mp3 xxx
http_access deny badurl2

#限制IP地址为192.168.168.50的客户机并发最大连接数为5。
acl client1 src 192.168.168.50
acl conn1 maxconn 5
http_access deny client1 conn1

#禁止192.168.142.0这个子网里所有的客户机在周一到周五的9:00到18:00上网。每天则为SMTWHFA,允许则为allow,上下午可以用2条分开
#S - Sunday, M - Monday, T - Tuesday, W - Wednesday, H - Thursday, F - Friday A - Saturday
acl clientnet2 src 192.168.142.0/24
acl worktime time MTWHF 9:00-18:00
http_access deny clientnet2 worktime

#禁止客户机下载*.mp3、*.exe、*.zip和*.rar类型的文件。
acl badfile1 urlpath_regex -i .mp3$ .exe$ .zip$ .rar$
http_access deny badfile1

#禁止QQ通过Squid代理上网。
acl qq url_regex -i tencent.com
http_access deny qq

#授权用户可以访问web
acl normal proxy_auth REQUIRED
http_access allow normal

#拒绝某些不安全的端口要求
http_access deny !Safe_ports

#拒绝连接到其他安全的SSL端口
http_access deny CONNECT !SSL_ports

#其他用户连接一律拒绝
http_access deny all
#資料保存多久.在此為一筆已在cache中的資料,若一個禮拜內沒人再來access, 則這筆資料會被清掉

reference_age 7 days

#最大下载文件大小
reply_body_max_size 20 MB



##上面第一行假如网址开头是ftp的话,那么在一天(1440分钟)后,
#假如proxy再次取用这个档案时,则cache内的数据会被更新!
# Add any of your own refresh_pattern entries above these.
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|?) 0 0% 0
refresh_pattern . 0 20% 4320

#squid.pid的路径
pid_filename /var/run/squid.pid

#错误提示语言,编译的时候已经给出了错误提示语言,现在可以更改
error_directory /usr/local/squid/share/errors/zh-cn
#上传文件大小限制
request_body_max_size 20 KB

访问控制说明:
为了使用控制功能,必须先设置 ACL。ACL 声明的格式如下:
acl acl_element_name type_of_acl_element values_to_acl
acl 列表名称 控制方式 控制目标
下面列出一些重要的 ACL类型(type_of_acl_element)
l src : 源地址(即客户机IP地址)
l dst : 目标地址(即服务器IP地址)
l srcdomain : 源名称 (即客户机名称)
l dstdomain : 目标名称 (即服务器名称)
l time : 一天中的时刻和一周内的一天
l url_regex : URL 规则表达式匹配
l urlpath_regex: URL-path 规则表达式匹配,略去协议和主机名
l proxy_auth : 通过外部程序进行用户验证
l maxconn : 单一 IP 的最大连接数
注:
1. acl_element_name 可以是任一个在 ACL 中定义的名称。
2. 任何两个 ACL 元素不能用相同的名字。
3. 每个 ACL 由列表值组成。当进行匹配检测的时候,多个值由逻辑或运算连接;换言之,即任一 ACL的值被匹配,则这个 ACL即被匹配。
4. 并不是所有的 ACL都能使用访问列表中的全部类型。
5. 不同的 ACL写在不同行中,Squid 将把它们组合在一个列表中。
我们可以使用许多不同的访问列表。我们常用的是http_access: 允许 HTTP 访问,这个是主要的访问控制列表。
访问列表的规则由一些类似 'allow' 或 'deny' 的关键字构成,用以允许或拒绝向特定或一组 ACL 元素提供服务。
注:
1. 这些规则按照它们的排列顺序进行匹配检测,一旦检测到匹配的规则,匹配检测就立即结束。
2. 一个访问列表可以又多条规则组成。
3. 如果没有任何规则与访问请求匹配,默认动作将与列表中最后一条规则对应。
4. 一个访问条目中的所有元素将用逻辑与运算连接:
http_access Action 声明1 AND 声明2 AND 声明 OR.
http_access Action 声明3
多个 http_access 声明间用或运算连接,但每个访问条目的元素间用与运算连接。
5. 请记住列表中的规则总是遵循由上而下的顺序。






6、增加用户&组,修改权限:
先使用cat /etc/passwd | grep squid
看看有没有用户squid,没有则增加
增加组
pw groupadd squid
增加用户,给他加入squid组
pw adduser squid -g squid -s /nonexistent
修改目录用户权限
chown -Rf squid /usr/local/squid
修改目录组权限
chgrp -Rf squid /usr/local/squid
修改权限
chmod -R 777 /usr/local/squid
chmod 777 /usr/local/squid/var/log/


7.初始化缓存空间
/usr/local/squid/sbin/squid -z 初始化缓存空间
初始化cache目录,只需在第一次启动squid服务之前执行(在初次运行squid之前,或者无论何时你增加了新的cache_dir,你必须初始化cache目录。)


8.启动Squid
/usr/local/squid/sbin/squid


9.开机启动
ee /etc/rc.local
增加
/usr/local/squid/sbin/squid
或者
/usr/local/squid/sbin/squid -sD
或者
ee /etc/rc.conf
/usr/local/squid/sbin/squid


10.squid常用命令:
/usr/local/squid/etc/squid.conf 配置文件位置
/usr/local/squid/sbin/squid -z 初始化缓存空间
/usr/local/squid/sbin/squid 启动
/usr/local/squid/sbin/squid -k shutdown 停止
/usr/local/squid/sbin/squid -k reconfigure 重新载入配置文件
/usr/local/squid/sbin/squid -k rotate 轮循日志
cat /usr/local/squid/var/logs/access.log 查看log







禁止qq代理(只允许部分ip可以上)
acl ip src 192.168.168.200
#acl qqip dst "/usr/local/squid/etc/acl/qqip.txt"
#http_access deny !ip qqip

不能上qq.com和tencent.com
acl badurl1 url_regex -i qq.com
http_access deny badurl1

acl qq url_regex -i tencent.com
http_access deny qq


qqip.txt内容
获取方式
现去官方网站下一个新的QQ,然后,安装好,先登录一次。
然后,把电脑的网线拔下来,你就会看到QQ登录了半天没有登录完成,这个时候会有一个错误信息,你点一下就可以看到QQ登录的全过程了。

219.133.60.206
112.95.240.110
219.133.63.54
58.60.14.242
121.14.100.89
112.90.141.105
119.147.18.52
58.60.9.66
58.251.62.17
58.251.62.77
219.133.49.8
219.133.60.174
219.133.49.7
219.133.48.104
58.60.14.51
112.95.240.111
113.108.80.249
58.60.14.195
221.236.11.35
219.133.51.243
119.147.12.198
121.14.74.246
121.14.101.193
112.90.138.187
112.95.240.162
121.14.95.42




禁止下载可以通过URL方式避免
限制特定用户才能上MSN
方法1
acl msnip src 192.168.168.20
acl CONNECT method CONNECT
http_access !deny msnip CONNECT

方法2
acl msnip src 192.168.168.20
acl msnmessenger req_mime_type ^application/x-msn-messenger$
http_access !deny msnip CONNECT

方法3
acl msn url_regex -i gateway.dll
http_access deny !msnip msn

同时禁止webmsn
acl webmsn url_regex -i webmsn.msn.com.tw live.com live.cn meebo.com
http_access deny webmsn


参考:
http://www.hijava.org/tool/how-toinstall-squid-on-linux
http://www.linux.gov.cn/netweb/squid.htm
http://bbs.chinaunix.net/viewthread.php?tid=959656
http://blog.sina.com.cn/s/blog_417b97470100kaap.html

http://www.likui.cn/log/?action=show&id=14

[@more@]http://www.likui.cn/log/?action=show&id=14

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29207/viewspace-1042889/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2012-01-14

  • 博文量
    15
  • 访问量
    49469