您的位置:首页 > 运维架构 > 反向代理

squid加速WEB支持虚拟主机配置心得体会

2008-11-14 21:24 706 查看
 本人的一个朋友是做网站的,一起托管了一台WEB服务器,日流量上万,上面开设了很多个虚拟主机,呵呵还有我的ynho.com,近日装上Squid 进行WEB加速,Squid 和Apache均在同一台服务器上面,效果非常明显,确实,弄这个东西花费了我不少时间,几个通宵的研究,同时也很感谢在CU上面的朋友热心帮助,下面是我的一些心得。
我们的服务器基本配置是:
INTEL 双核 2.8g , 2G内存,160G SATA硬盘  100M宽带  操作系统:freebsd
http://www.squid-cache.org下载最新的SQUID软件
安装: ./configure --with-maxfd=65536
这个--with-maxfd参数是增大squid文件描述符到65536
安装完毕后开始配置/usr/local/squid/etc/squid.conf
visible_hostname www.ynho.com
http_port xx.xx.xx.xx:80 vhost vport
#xx.xx.xx.xx为这台服务器的IP地址
icp_port 0
cache_mem 400 MB
#设置Squid所能使用的内存共400MB,这个值因人而异
cache_swap_low 90
cache_swap_high 95
maximum_object_size 20000 KB
#最大缓存文件大小,超过这个值则不缓存,这个值因人而异
maximum_object_size_in_memory 4096 KB
#装入内存缓存的文件大小,这个值对Squid的性能影响比较大,因为默认值是8K,超过8K的文件都不装入内存,而实际应用中很多网页和图片等都超过8KB, 个人认为如果缓存不装入内存而存在磁盘上,性能和apache直接读取磁盘文件没什么区别,甚至不如直接访问apache,现在设置成小于4兆的文件通通装入内存缓存.
cache_dir ufs /tmp1 10000 16 256
#磁盘缓存的类型和目录,大小,一二级目录的设置,这里磁盘缓存大小是10G
cache_store_log none
#这个设置是不记录store.log
emulate_httpd_log on
#打开emulate_httpd_log选项,将使Squid仿照Aapche的日志格式
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
#日志格式combined的设置
pid_filename /var/log/squid/squid.pid
cache_log /var/log/squid/cache.log
access_log /var/log/squid/access.log combined
#这里是设置pid和日志文件的位置,因人而异,同时日志格式是combined,awstats可以直接调用分析了
acl all src 0.0.0.0/0.0.0.0
acl QUERY urlpath_regex cgi-bin .php .cgi .avi .wmv .rm .ram .mpg .mpeg .zip .exe
cache deny QUERY
#设置不想缓存的目录或者文件类型
acl picurl url_regex -i /.bmp$ /.png$ /.jpg$ /.gif$ /.jpeg$
acl mystie1 referer_regex -i 1.ynho.com
http_access allow mystie1 picurl
acl mystie2 referer_regex -i 2.ynho.com
http_access allow mystie2 picurl
#设置防图片盗链的,其中1.ynho.com,和2.ynho.com分别是虚拟主机的域名,referer中必须包含有aaa或者bbb的域名才能访问图片
acl nullref referer_regex -i ^$
http_access allow nullref
acl hasref referer_regex -i .+
http_access deny hasref picurl
#设置允许直接访问图片和拒绝referer中没有包含aaa或着bbb的访问图片
cache_peer xx.xx.xx.xx parent 81 0 no-query originserver login=PASS
#xx.xx.xx.xx还是本机服务器的IP,81则是apache的端口,如果你的虚拟主机有用户名和密码保护起来的目录必须设置login=PASS,否则认证会失效
cache_effective_user nobody
cache_effective_group nobody
#squid使用的用户组和用户名
squid配置完成!
建立缓存和日志目录,并改变权限使squid能写入
mkdir /tmp1
mkdir /var/log/squid
chown -R nobody:nobody /tmp1
chmod 666 /tmp1
chown -R nobody:nobody /var/log/squid
-----------------------
Apache需要改动的配置
Port 81

#要把端口改为81

NameVirtualHost xx.xx.xx.xx:81
#本台主机IP和端口

虚拟主机配置
<VirtualHost xx.xx.xx.xx>
    ServerAdmin xxx@yahoo.com
    DocumentRoot /home/aaa/www
    ServerName aaa.com
    ServerAlias www.aaa.com
    ScriptAlias /cgi-bin/ "/home/aaa/cgi-bin/"
    <Directory />
      Options Includes FollowSymLinks
      AllowOverride All
    </Directory>
</VirtualHost>

如果还有别的虚拟主机请参照上面设置重启apache : apachectl restart

----------------------------------------------------------
首次运行squid要先建立缓存

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

启动squid

echo "65535" > /proc/sys/fs/file-max
ulimit -HSn 65535
/usr/local/squid/sbin/squid

大家最好把这几句话放到squid启动脚本里面,这样才会获得65536文件描述符

最好还编辑/etc/hosts 文件
添加以下内容
xx.xx.xx.xx ynho.com www.ynho.com
这样免去查询DNS,速度也快一些
现在netstat 看一下端口启动没有
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息