利用squid实现反向代理(同时具备内网代理)

原贴:http://www.cnfug.org/journal/systems/2004/000079.html

利用squid实现反向代理(同时具备内网代理)
  •  
  • 文章正文
  • 网友评论(2)
  • 发表评论
  • 推荐给好友
  • 文章下载
  • 打印

Matthew(黑夜编码人) < matthew at cnfug.org >

我的站点上的所有文件都是静态html,为了进一步的加快网页速度,于是使用squid实现反向代理缓存功能。

@ 安装squid
为了偷懒就直接使用ports安装,因为我将所有的服务器系统都安装在/s分区中,所以设置了PREFIX为/s/squid

# cd /usr/ports/www/squid
# setenv PREFIX /s/squid
# make install clean

安装完成后编辑/s/squid/etc/squid/squid.conf,我的配置内容如下:



########## Base control ##########

cache_mgr webmaster

cache_effective_user squid

cache_effective_group squid

visible_hostname sleepcat.3322.org

http_port 127.0.0.1:3128

icp_port 0

cache_dir ufs /usr/cache 300 16 256

cache_access_log /dev/null

cache_log /dev/null

cache_store_log /dev/null

error_directory /s/squid/etc/squid/errors/Simplify_Chinese

icon_directory /s/squid/etc/squid/icons

mime_table /s/squid/etc/squid/mime.conf

coredump_dir /s/squid/squid/cache

pid_filename /s/squid/squid/logs/squid.pid

hosts_file /etc/hosts

unlinkd_program /s/squid/libexec/squid/unlinkd

########## Performance control ##########

cache_mem 8 MB

cache_swap_low 90

cache_swap_high 95

maximum_object_size 4096 KB

minimum_object_size 0 KB

maximum_object_size_in_memory 8 KB

ipcache_size 1024

ipcache_low 90

ipcache_high 95

fqdncache_size 1024

cache_replacement_policy lru

memory_replacement_policy lru

emulate_httpd_log off

log_ip_on_direct on

log_mime_hdrs off

dns_timeout 2 minutes

request_header_max_size 10 KB

request_body_max_size 0 KB

refresh_pattern ^ftp: 1440 20% 10080

refresh_pattern ^gopher: 1440 0% 1440

refresh_pattern . 0 20% 4320

negative_ttl 5 minutes

positive_dns_ttl 6 hours

negative_dns_ttl 1 minute

connect_timeout 1 minute

read_timeout 15 minutes

request_timeout 5 minutes

client_lifetime 1 day

half_closed_clients on

maximum_single_addr_tries 1

uri_whitespace strip

ie_refresh off



########## Access control ############

acl all src 0.0.0.0/0.0.0.0

http_access allow all

http_reply_access allow all

hierarchy_stoplist cgi-bin ?



########## Accelator control ############

httpd_accel_host virtual

httpd_accel_port 80

httpd_accel_with_proxy on

httpd_accel_uses_host_header on



########## Misc control ###########

ftp_user Squid@

ftp_list_width 32

ftp_passive on

ftp_sanitycheck on


初始化cache

# /s/squid/sbin/squid -z

启动squid

# /s/squid/etc/rc.d/squid.sh start

@ 设置apache
因为要对外部实现反向透明代理,所以apache不能再在外网IP的80端口服务,我将它设置到127.0.0.1的80端口,修改httpd.conf中的Listen为:

Listen 127.0.0.1:80

刷新apache配置:
# apachectl restart

@ 重定向外网IP的80端口访问
我使用mpd进行PPPoE拨号,所以外网接口为ng0,将所有发往ng0上的80端口请求转发到127.0.0.1的3128端口上,即转发到squid的服务端口。转发规则如下:

rdr ng0 0.0.0.0/0 port 80 -> 127.0.0.1 port 3128

同时为了使rdr规则转换后的包能通过防火墙,增加如下ipfilter过滤规则:
pass in quick on ng0 proto tcp from any to 127.0.0.1 port 3128 keep state

@ 设置/etc/hosts文件
现在我们已经实现下面的功能:
client ------> ng0:80 ------> 127.0.0.1:3128
接下来就要使squid访问真正的服务以实现反向代理缓存。刚才我已经将apache设置到在127.0.0.1:80端口监听,现在只要让squid访 问127.0.0.1:80便可,由于我们在squid的配置文件中设置了hosts文件的位置为/etc/hosts,所以我们只要在hosts文件中 设置所有我们的WWW主机(包括虚拟主机)指向127.0.0.1便可。

127.0.0.1 matthew.3322.org sleepcat.3322.org mirrors.2288.org

同时请确保你的主机搜索顺序为 file bind,如果不是,请执行以下命令:
# echo "file" > /etc/host.conf
# echo "bind" >> /etc/host.conf"

现在整个流程如下:
client ------> ng0:80 ------> 127.0.0.1:3128 -------> 127.0.0.1:80
127.0.0.1:80 ----------> 127.0.0.1:3128 ----------> ng0:80 ----------> client

现在反向代理已经建成,刷新ipfilter和ipnat规则,启动squid,你会发现从外网访问你的网站的速度明显比以前快了很多,而且也支持虚拟主机。

网友评论  以下本论与CNFUG(China FreeBSD User Group)立场无关
网友:skylove 发表时间:2004年07月09日

如果把squid开在80,apache开到另一个端口。。。是不是能少走一次??
client ------> ng0:80 ------> 127.0.0.1:80 (这里squid 监听ng0 的80端口,apache监听127.0.0.1的80端口),这样的话理论上节约一个步骤出来??

网友:Matthew 发表时间:2004年07月09日

是的,如果只对外代理是可以的,但如果要对内代理就的话就需要相应的修改ipfilter的rdr规则。

我也说几句




推荐给好友

  


文章下载

本功能正在开发中,目前不能使用,敬请原谅。

√ 期刊在线投稿: http://www.lstv.net.cn/journal/contribute.html

√ 本文打印于《CNFUG期刊》,欢迎访问 http://www.cnfug.org 获取更多技术文章。

<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> width="728" scrolling="no" height="90" frameborder="0" name="google_ads_frame" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-7874063323015706&dt=1189615971845&lmt=1189615970&format=728x90_as&output=html&correlator=1189615971845&url=http%3A%2F%2Fwww.cnfug.org%2Fjournal%2Fsystems%2F2004%2F000079.html&color_bg=FFFFFF&color_text=000000&color_link=0066CC&color_url=008000&color_border=CCCCCC&ad_type=text_image&ref=http%3A%2F%2Fwww.google.cn%2Fsearch%3Fcomplete%3D1%26hl%3Dzh-CN%26ie%3DGB2312%26q%3Dsquid%2B%25B7%25B4%25CF%25F2%25B4%25FA%25C0%25ED%26btnG%3DGoogle%2B%25CB%25D1%25CB%25F7%26meta%3D%26aq%3Dt%26oq%3Dsquid%2B&cc=100&ga_vid=470891839.1189615972&ga_sid=1189615972&ga_hid=234907000&flash=9&u_h=800&u_w=1280&u_ah=776&u_aw=1280&u_cd=24&u_tz=480&u_his=1&u_nplug=2&u_nmime=3" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true"> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值