Chinaunix

标题: [操作实例] squid-2.6之Web反向代理加速实做/防盗链/防盗用/防爬虫 [打印本页]

作者: HonestQiao    时间: 2006-07-26 12:10
标题: [操作实例] squid-2.6之Web反向代理加速实做/防盗链/防盗用/防爬虫
[操作实例] squid-2.6.STABLE1-20060726之Web加速实做


  2.6相对2.5有了一些改变,大家可以去看:http://www.squid-cache.org/Versions/v2/2.6/squid-2.6.STABLE1-20060726-RELEASENOTES.html#s2

  1. 下载squid2.6 http://www.squid-cache.org/Versions/v2/2.6/squid-2.6.STABLE1-20060726.tar.gz

  2. 安装:

  1. wwwtest137#tar xzvf squid-2.6.STABLE1-20060726.tar.gz
  2. wwwtest137#cd squid-2.6.STABLE1-20060726
  3. wwwtest137#configure --prefix=/usr/local/squid --enable-dlmalloc --with-pthreads --enable-poll --disable-internal-dns --enable-stacktrace --enable-removal-policies="heap,lru" --enable-delay-pools --enable-storeio="aufs,coss,diskd,ufs"
  4. wwwtest137#make
  5. wwwtest137#make install
复制代码

  安装完成了。
  因为是测试的,所以相关的参数可能并不是比较完善的,但是使用是没有问题的。

  3. 配置:以下是部分关键配置,其他与2.5的相同

  1. #squid.conf
  2. #服务器IP 192.168.1.1
  3. #监听服务器的80端口,透明代理,支持域名和IP的虚拟主机
  4. http_port 192.168.1.1:80 transparent vhost vport
  5. #最新2.6使用http_port 192.168.1.1:80 vhost vport

  6. #限制同一IP客户端的最大连接数
  7. acl OverConnLimit maxconn 16
  8. http_access deny OverConnLimit

  9. #防止天涯盗链,转嫁给百度
  10. acl tianya referer_regex -i tianya
  11. http_access deny tianya
  12. deny_info http://www.baidu.com/logs.gif tianya

  13. #防止被人利用为HTTP代理,设置允许访问的IP地址
  14. acl myip dst 192.168.1.1
  15. http_access deny !myip

  16. #防止百度机器人爬死服务器
  17. acl AntiBaidu req_header User-Agent Baiduspider
  18. http_access deny AntiBaidu

  19. #允许本地管理
  20. acl Manager proto cache_object
  21. acl Localhost src 127.0.0.1 192.168.1.1
  22. http_access allow Manager Localhost
  23. http_access deny Manager

  24. #仅仅允许80端口的代理
  25. acl Safe_ports port 80 # http
  26. http_access deny !Safe_ports
  27. http_access allow all

  28. #Squid信息设置
  29. visible_hostname www.test137.com
  30. cache_mgr webmaster@test137.com

  31. #基本设置
  32. cache_effective_user squid
  33. cache_effective_group squid
  34. tcp_recv_bufsize 65535 bytes

  35. #2.5的反向代理加速配置
  36. #httpd_accel_host 127.0.0.1
  37. #httpd_accel_port 80
  38. #httpd_accel_single_host on
  39. #httpd_accel_uses_host_header on
  40. #httpd_accel_with_proxy on
  41. #2.6的反向代理加速配置
  42. #代理到本机的80端口的服务,仅仅做为原始内容服务器
  43. cache_peer 127.0.0.1 parent 80 0 no-query originserver

  44. #错误文档
  45. error_directory /usr/local/squid/share/errors/Simplify_Chinese

  46. #单台使用,不使用该功能
  47. icp_port 0


复制代码


  4. http服务器配合设置:
http服务器,监听到127.0.0.1的80端口。

  5. 数据走向:
访问者=>192.168.1.1:80=>127.0.0.1:80

  6. 测试:
/usr/local/squid/sbin/squid -z
/usr/local/squid/sbin/squid -NCd1
  好了,现在访问你的服务器看看,已经好了。

  为了测试是否可用,把http服务器给停了,你就可以看到squid2.6的信息了。

  另外,我们设置:
SQUID监听外部IP的80端口
HTTP服务器监听本机127.0.0.1的80端口
这样子不用任何防火墙参与,即可完成web反向代理加速。

  

[ 本帖最后由 HonestQiao 于 2007-1-12 15:41 编辑 ]
作者: LuckSnail    时间: 2006-07-26 13:11
先顶 再看 !!
作者: LuckSnail    时间: 2006-07-27 10:51
#防止天涯盗链,转嫁给百度
acl tianya referer_regex -i tianya
http_access deny tianya
deny_info tianya
这是拒绝了天涯的refere过来的连接

我想建立一条规则,只允许天涯的referer或则是无referer 都可以正常访问,不允许别的域名referer过来的访问。
这个规则该怎么写?
作者: phpman    时间: 2006-07-27 20:57
2.6的错误页,时间还是GMT的吧?
作者: HonestQiao    时间: 2006-07-27 21:01
原帖由 LuckSnail 于 2006-7-27 10:51 发表
#防止天涯盗链,转嫁给百度
acl tianya referer_regex -i tianya
http_access deny tianya
deny_info http://www.baidu.com/logs.gif tianya
这是拒绝了天涯的refere过来的连接

我想建立一条规 ...


那也可以的啊。

acl tianya referer_regex -i tianya
http_access allow tianya

acl nullref referer_regex -i ^$
http_access allow nullref

acl hasref referer_regex -i .+
http_access deny hasref
deny_info http://www.baidu.com/logs.gif hasref

[ 本帖最后由 HonestQiao 于 2006-7-28 09:23 编辑 ]
作者: kaka_sun    时间: 2006-07-28 10:30
太棒了,顶
作者: ability    时间: 2006-07-28 13:07
好东西!
作者: uingei    时间: 2006-07-29 05:33
2.6好像做反带还不成熟,虽然配置上和3差不多
作者: seacaptain    时间: 2006-07-31 12:26
squid2.6配置反向代理,启动就报错
#squid.conf
http_port 80 accel

squid: Bungled squid.conf line 2: http_port 80 accel

但是使用vhost选项就没问题,不知道什么原因导致?
作者: uingei    时间: 2006-08-01 05:12
http_port用accel是squid3才可以
作者: seacaptain    时间: 2006-08-01 08:48
是这样吗?不过的却是看的3.0的手册
作者: bend    时间: 2006-08-03 09:58
好文章,先收藏
作者: wpl12    时间: 2006-08-11 16:59
加速能力在哪里体现?缓存所有的页面?页面更新了怎么办?
作者: 四不象    时间: 2006-08-11 19:24
这爬虫防得可不科学呀

这对SEO大大的不利呀

最好能判断单位时间内被爬虫爬过的次数
作者: HonestQiao    时间: 2006-08-13 00:47
原帖由 四不象 于 2006-8-11 19:24 发表
这爬虫防得可不科学呀

这对SEO大大的不利呀

最好能判断单位时间内被爬虫爬过的次数



那可以让baidu爬虫的最大连接数限制住也可以的.
作者: HonestQiao    时间: 2006-08-14 16:43
原始短消息: 你好 有个问题请问
FATAL: Bungled squid.conf line 4: http_port 192.168.80.241:8
vport
Squid Cache (Version 2.6.STABLE2-NT): Terminated abnormally.

看了你的文章
squid-2.6之Web反向代理加速实做/防盗
为什么我在启动squid的时候 出现这个问题
服务器ip 也就是我本机 是:192.168.80.241
谢谢


你的配置,是:
http_port 192.168.80.241:8
还是:
http_port 192.168.80.241:80
作者: 爱是福    时间: 2006-08-14 17:21
刚刚自己检查了一下
之前的配置 是 直接拿 2.15 的配置修改的
这句话 也没有问题
是http_port 192.168.80.241:80 transparent vhost vport

后来重新试了一下 直接把你的 所有的配置文件 在本机运行

1 首先 是 “all ” 没定义 于是 自己加了个定义
   acl all src 0.0.0.0/0.0.0.0
2 然后squid 起来了
   试着做 反向代理
  在 ie 输入 www.sohu.com
  确报 如下错误:
   The following error was encountered:

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.

Your cache administrator is webmaster@test137.com.


想问楼主 原因是?
作者: HonestQiao    时间: 2006-08-15 09:03
原帖由 爱是福 于 2006-8-14 17:21 发表
刚刚自己检查了一下
之前的配置 是 直接拿 2.15 的配置修改的
这句话 也没有问题
是http_port 192.168.80.241:80 transparent vhost vport

后来重新试了一下 直接把你的 所有的配置文件 在本机运行

...




#防止被人利用为HTTP代理,设置允许访问的IP地址
acl myip dst 192.168.1.1
http_access deny !myip
作者: zzx03    时间: 2006-08-15 15:53
请问一下 正向代理和反向代理的开与关在哪设置?
作者: LinuxWalker    时间: 2006-08-29 10:42
2.6的反向代理加速能不能使用正则表达式啊,要不然对一些网站都要添加好几十条的规则,
那太麻烦了,而且对每个网站的新域名的添加也不方便.
例如sina添加一个sample.sina.com服务器要使用反代,
如果我有一百个代理服务器的话,那每台机器都要改一遍,管理上太麻烦了.
作者: 风流涕淌    时间: 2006-09-08 09:51
先顶一下,马上实作
作者: 风流涕淌    时间: 2006-09-08 17:00
以实作完成,没有问题,只是没有定义all
在80端口下加
  1. acl all src 0.0.0.0/0.0.0.0
复制代码

作者: 风流涕淌    时间: 2006-09-18 13:43
原帖由 phpman 于 2006-7-27 20:57 发表
2.6的错误页,时间还是GMT的吧?



这个有办法弄好吗
作者: liyangboy    时间: 2006-09-27 21:10
wwwtest137#tar xzvf squid-2.6.STABLE1-20060726.tar.gz
wwwtest137#cd squid-2.6.STABLE1-20060726
wwwtest137#configure --prefix=/usr/local/squid --enable-dlmalloc --with-pthreads --enable-poll --disable-internal-dns --enable-stacktrace --enable-removal-policies="heap,lru" --enable-delay-pools --enable-storeio="aufs,coss,diskd,ufs"
wwwtest137#make
wwwtest137#make install

这些怎么安装 windows 2003 能够安装码?
作者: 四不象    时间: 2006-09-28 09:57
原帖由 liyangboy 于 2006-9-27 21:10 发表
wwwtest137#tar xzvf squid-2.6.STABLE1-20060726.tar.gz
wwwtest137#cd squid-2.6.STABLE1-20060726
wwwtest137#configure --prefix=/usr/local/squid --enable-dlmalloc --with-pthreads --enable-poll --dis ...



windows下有一个叫SquidNT的软件

http://www.serassio.it/SquidNT.htm
作者: james.liu    时间: 2007-01-08 13:00
...

[ 本帖最后由 james.liu 于 2007-1-8 15:46 编辑 ]
作者: phpman    时间: 2007-01-08 15:25
原帖由 LinuxWalker 于 2006-8-29 10:42 发表
2.6的反向代理加速能不能使用正则表达式啊,要不然对一些网站都要添加好几十条的规则,
那太麻烦了,而且对每个网站的新域名的添加也不方便.
例如sina添加一个sample.sina.com服务器要使用反代,
如果我有一百个代 ...

可以把squid.conf中经常变动的部分(比如加速的域名)分离成单个文件,这样即使你有上千台squid也不怕了~
作者: powerv_cu    时间: 2007-07-05 01:16
出现如下错误:
[root@host etc]# /usr/local/squid/sbin/squid -z
FATAL: Can't parse configuration token: '%'

Squid Cache (Version 2.6.STABLE13-20070704): Terminated abnormally.
CPU Usage: 0.004 seconds = 0.002 user + 0.002 sys
Maximum Resident Size: 0 KB
Page faults with physical i/o: 0
已放弃

我的安装是这样的:
./configure --prefix=/usr/local/squid --enable-async-io=100 --disable-delay-pools \
--disable-mem-gen-trace --disable-useragent-log --enable-kill-parent-hack --disable-arp-acl \
--enable-epoll --disable-ident-lookups --enable-snmp --enable-large-cache-files --with-large-files

我的服务器IP是公网固定IP。如:210.21.77.77
WEB也是同一台机器。
Version 2.6.STABLE13
我设置为:
# Squid normally listens to port 3128
#http_port 3128
http_port 210.21.77.77:80  vhost vport

#防止被人利用为HTTP代理,设置允许访问的IP地址
acl myip dst 210.21.77.77
http_access deny !myip

#仅仅允许80端口的代理
acl Safe_ports port 80 # http
http_access deny !Safe_ports
http_access allow all

#代理到本机的80端口的服务,仅仅做为原始内容服务器
cache_peer 210.21.77.77 parent 80 0 no-query originserver

请问正确吗?是否这样还需要通过防护墙设置呢?

[ 本帖最后由 powerv 于 2007-7-5 01:18 编辑 ]
作者: kaka_sun    时间: 2007-07-05 10:46
是你的squid.conf配置的问题
作者: powerv_cu    时间: 2007-07-05 16:26
请教楼上的,如何更正?修改哪部分?谢谢了。
作者: jun821    时间: 2007-07-09 11:26
你这都用80
是不是apache和squid不在同一台机子上,如果在同一台机子上要怎么设置了
作者: HonestQiao    时间: 2007-07-10 14:01
原帖由 jun821 于 2007-7-9 11:26 发表
你这都用80
是不是apache和squid不在同一台机子上,如果在同一台机子上要怎么设置了


同一台机子的话,可以:
1. squid、apache运行在不通的ip
2. squid、apache运行在不通的端口
作者: gaochong    时间: 2007-07-17 13:42
标题: 回复 #9 seacaptain 的帖子
accel           Accelerator mode. Also needs at least one
                   of vhost/vport/defaultsite.
作者: gaochong    时间: 2007-07-17 14:06
标题: 回复 #32 HonestQiao 的帖子
选择1
作者: HonestQiao    时间: 2007-07-17 15:41
原帖由 gaochong 于 2007-7-17 14:06 发表
选择1


那是最简单的设置了啊。

每个都使用一个ip的80端口不就得了?
作者: gaochong    时间: 2007-07-17 16:20
标题: 回复 #35 HonestQiao 的帖子
而且从安全方面考虑,也应该选择1。如下:

If you do not have a separate computer with Squid installed, you can run Squid on the source server itself. Because both applications cannot have the same IP address or port number, you need to bind the source server to a different IP address or change the port number. It is better to change the IP address instead of the port number. This is because when the source server generates an error message, it may open an incorrect port, which results in conflicting port numbers.

If the source server generates an HTTP redirect message on an end user’s request, the source server adds the nonstandard port number to the Uniform Resource Indicator (URI), such as:

http://www.abc.com:81

As a result, if the client receives a response, the client connects to the nonstandard port and bypasses Squid. If you need to run Squid on the same computer as the source server, it is better to configure the source server to listen to the loopback address to avoid conflicting port numbers.
作者: fairysky    时间: 2007-07-30 17:08
版主大人,我想请教一下,你上面squid配置如何实现一对多呢?(您的配置似乎只能代理一台APACHE服务器)
就是说,如何在多台不同域名的web服务器前面放一台squid做代理呢?谢谢......

已经在看您的文章:
http://bbs.chinaunix.net/viewthread.php?tid=886235&highlight=虚拟主机

谢谢了!

[ 本帖最后由 fairysky 于 2007-7-30 17:52 编辑 ]
作者: cifan    时间: 2007-08-06 17:28
good
我整个配置文件
就只有楼主写的那些
有没有问题?
作者: yibinboy    时间: 2007-10-29 13:58
请问一下.如何实现这样的功能.一般情况下.反向代理的域名都能被访问.现在我想让访问都从指的网站才能访问被代理的网站.如何实现
流程:指定网站----->被代理网站
不能直接访问被代理的网站
作者: 网络    时间: 2008-04-01 04:00
原帖由 phpman 于 2007-1-8 15:25 发表

可以把squid.conf中经常变动的部分(比如加速的域名)分离成单个文件,这样即使你有上千台squid也不怕了~


这个方法也劳民伤财啊

还有别的办法吗?
作者: qczl1224    时间: 2008-04-19 11:06
Starting Squid Cache version 2.6.STABLE19 for i686-pc-winnt...
2008/04/19 10:39:12| Running as Squid Windows System Service on Windows Server 2003
2008/04/19 10:39:12| Service command line is:
2008/04/19 10:39:12| Process ID 11904
2008/04/19 10:39:12| With 2048 file descriptors available
2008/04/19 10:39:12| With 2048 CRT stdio descriptors available
2008/04/19 10:39:12| Windows sockets initialized
2008/04/19 10:39:12| Using select for the IO loop
2008/04/19 10:39:12| Performing DNS Tests...
2008/04/19 10:39:12| Successful DNS name lookup tests...
2008/04/19 10:39:12| DNS Socket created at 0.0.0.0, port 3993, FD 5
2008/04/19 10:39:12| Adding nameserver 218.2.135.1 from Registry
2008/04/19 10:39:12| User-Agent logging is disabled.
2008/04/19 10:39:12| Referer logging is disabled.
2008/04/19 10:39:12| Unlinkd pipe opened on FD 7
2008/04/19 10:39:12| Swap maxSize 10240000 KB, estimated 787692 objects
2008/04/19 10:39:12| Target number of buckets: 39384
2008/04/19 10:39:12| Using 65536 Store buckets
2008/04/19 10:39:12| Max Mem  size: 2097152 KB
2008/04/19 10:39:12| Max Swap size: 10240000 KB
2008/04/19 10:39:12| Local cache digest enabled; rebuild/rewrite every 3600/3600 sec
2008/04/19 10:39:12| Store logging disabled
2008/04/19 10:39:12| Rebuilding storage in c:/squid/var/cache (CLEAN)
2008/04/19 10:39:12| Using Least Load store dir selection
2008/04/19 10:39:12| Set Current Directory to c:/squid/var/cache
2008/04/19 10:39:12| Loaded Icons.
2008/04/19 10:39:12| Accepting accelerated HTTP connections at 0.0.0.0, port 80, FD 12.
2008/04/19 10:39:12| Accepting HTCP messages on port 4827, FD 13.
2008/04/19 10:39:12| Accepting SNMP messages on port 3401, FD 14.
2008/04/19 10:39:12| Configuring Parent 127.0.0.1/82/0
2008/04/19 10:39:12| Ready to serve requests.
2008/04/19 10:39:13| temporary disabling (Not Found) digest from 127.0.0.1
2008/04/19 10:39:42| WARNING! Your cache is running out of filedescriptors
2008/04/19 10:40:00| WARNING! Your cache is running out of filedescriptors
2008/04/19 10:40:16| WARNING! Your cache is running out of filedescriptors
2008/04/19 10:40:33| WARNING! Your cache is running out of filedescriptors
2008/04/19 10:40:49| TCP connection to 127.0.0.1/82 failed
2008/04/19 10:40:49| TCP connection to 127.0.0.1/82 failed
2008/04/19 10:40:49| TCP connection to 127.0.0.1/82 failed

出现上面的问题有什么解决方法吗,
打开浏览器输入 127.0.0.1:80/
页面是可以访问的!
作者: ahsiao    时间: 2008-04-21 13:34
#限制同一IP客户端的最大连接数
acl OverConnLimit maxconn 16
http_access deny OverConnLimit

------------------------------------------
这里的连接是指什么?所有的UDP和TCP连接吗?有的电脑可以打开16个IE页面,有的只能开一个页面,有的连一个页面也开不了。但如果不限制连接数,打开网页却没问题。请教达人,是为什么?
作者: hahazhu0634    时间: 2008-06-03 14:03
验证完毕,很好!~

acl MyNetwork src 192.168.0.0/16
acl manager proto cache_object
acl localhost src 127.0.0.1/32
acl to_localhost dst 127.0.0.0/8
acl SSL_ports port 443
acl CONNECT method CONNECT
acl Safe_ports  port 80 21

http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow MyNetwork
icp_access allow all
http_access deny all
http_port 192.168.5.124:80 transparent vhost vport
hierarchy_stoplist cgi-bin ?
access_log /usr/local/squid/var/logs/access.log squid
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern (cgi-bin|\?)    0       0%      0
refresh_pattern .               0       20%     4320

icp_port 3130
coredump_dir /usr/local/squid/var/cache
cache_dir ufs /usr/squidcache 700 16 256
cache_peer 192.168.1.24 parent 80 0 no-query originserver
作者: ylcqen    时间: 2008-06-05 10:24
写得不错!




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2