在同一台机器上安装squid给apache加速

简单原理说明:

squid可以缓存经常访问的静态页面,图片等,因此加上squid以后,客户端再访问这些页面的时候就会直接从squid的缓存中读取,而不再去向apache请求了

服务器上先按照正常安装好apache,确保能够正常访问,然后安装squid

squid下载地址 (这是当前稳定版的最新一个)

解压缩

tar zxvf squid-2.5.STABLE10.tar.gz
cd squid-2.5.STABLE10

使用如下的命令行来配置squid

./configure --enable-useragent-log --enable-referer-log --disable-icmp --disable-delay-pools --enable-default-err-language=Simplify_Chinese --enable-err-languages="Simplify_Chinese English" --disable-internal-dns --enable-async-io=160 --enable-cahce-digests --enable-underscore --enable-kill-parent-hack --enable-gnuregex

关于每一项参数的说明,请参考./configure --help

然后make,确认编译正常没有报错信息,就make install 安装,默认的安装路径是/usr/local/squid

添加squid用户和组

groupadd squid
useradd squid -g squid -s /sbin/nologin

创建高速缓存目录和log目录

cd /var/
mkdir squid
cd squid
mkdir cache
mkdir logs
chown -R squid:squid /var/squid

修改squid配置文件squid.conf为以下内容

http_port 80
icp_port 0
hierarchy_stoplist cgi-bin ?
acl QUERY urlpath_regex cgi-bin \? \.asp \.php \.jsp \.cgi
no_cache deny QUERY
cache_mem 128 MB
log_icp_queries off
half_closed_clients off
cache_swap_low 60
cache_swap_high 95
maximum_object_size 1024 KB
minimum_object_size 0 KB
maximum_object_size_in_memory 8 KB
ipcache_size 1024
ipcache_low 90
ipcache_high 95
cache_dir ufs /var/squid/cache 4096 16 256
cache_access_log none
cache_log /var/squid/logs/cache.log
cache_store_log none
pid_filename /var/squid/logs/squid.pid
quick_abort_min 16 KB
quick_abort_max 16 KB
quick_abort_pct 95
connect_timeout 120 seconds
read_timeout 15 minutes
request_timeout 30 seconds
client_lifetime 60 minutes
pconn_timeout 120 seconds
dns_children 20
redirect_children 20
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i .html 0 40% 60 ignore-reload
refresh_pattern -i .hml 0 40% 60 ignore-reload
refresh_pattern -i .gif 10 40% 60 ignore-reload
refresh_pattern -i .swf 10 40% 60 ignore-reload
refresh_pattern -i .jpg 10 40% 60 ignore-reload
refresh_pattern -i .js$ 10 40% 60 ignore-reload
refresh_pattern -i .css 10 40% 60 ignore-reload
refresh_pattern . 0 20% 60
acl all src 0.0.0.0/0.0.0.0
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl to_localhost dst 127.0.0.0/8
acl SSL_ports port 443 563
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 563 # https, snews
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 Safe_ports port 8080
acl CONNECT method CONNECT
acl conncount maxconn 20
http_access deny conncount 
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow all
http_reply_access allow all
icp_access allow all
cache_effective_user squid
cache_effective_group squid
visible_hostname 192.168.18.114
redirect_rewrites_host_header off
httpd_accel_port 8080
httpd_accel_host virtual
httpd_accel_uses_host_header on
buffered_logs on
icp_access allow all
cache_mgr lilu@zhongsou.com
coredump_dir /var/squid/cache
logfile_rotate 2

为了能够和squid相配合,修改apache的配置文件httpd.conf

一,修改apache监听端口

port 8080

二,修改虚拟主机设置的端口,举例如下

NameVirtualHost 192.168.18.114:8080
<VirtualHost 192.168.18.114:8080>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /zhongsou
ServerName test.test.com
</VirtualHost>
<VirtualHost 192.168.18.114:8080>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /usr/local/apache/htdocs
ServerName test.example.com
</VirtualHost>

所有的虚拟主机都要修改

其次为了能够正确记录来访者的IP,需要修改CustomLog

将%h改为%{X-Forwarded-For}i,其余不变

然后分别起动apache和squid

/usr/local/apache/bin/apachectl start
/usr/local/squid/sbin/squid -z
/usr/local/squid/sbin/squid -D

然后netstat -na|grep LISTEN查看一下80端口和8080端口是否已经处在监听状态了

然后就可以进行测试了。