構築/Linux サーバ/squid/Version2.6 - http accelerator mode ( コンテンツキャッシュサーバ )
提供: スゴイようぃっきー!
構築 / Linux サーバ / squid / Version2.6 - http accelerator mode ( コンテンツキャッシュサーバ )
目次 |
概要
- squid を利用したコンテンツキャッシュサーバを構築する。
- squid サーバは冗長化を前提とする。また、ネットワークの概略として下図のようにActive/Stanby構成のlvsロードバランサ、Apache with mod_proxy_balancerによるラウンドロビン負荷分散を用いて、冗長化したsquidサーバに接続することを想定しています。
- lvsについては「構築/Linux サーバ/lvs」を、Apache with mod_proxy_balancerについては「構築/Linux サーバ/Web Server/2.a apache(with mod proxy balancer)によるロードバランサ構築(L7)」を参照ください。
特に、本ページでのsquid冗長構成は、前段にapacheのproxyバランサを挟むことを想定しており、httpd.conf等にてwebサーバへの直接アクセスか、squidへのキャッシュアクセスかを制御するようにしています。
- lvsについては「構築/Linux サーバ/lvs」を、Apache with mod_proxy_balancerについては「構築/Linux サーバ/Web Server/2.a apache(with mod proxy balancer)によるロードバランサ構築(L7)」を参照ください。
- 図中の各サーバのIPアドレスは小生宅の商用環境のものですが、本ページ内に記載されているIPアドレスは小生宅の開発環境用のアドレスになります。それぞれ以下のように読み替えください。
Webサーバ 192.168.3.3[12] → 192.168.3.12[12]
squidサーバ 192.168.3.5[12] → 192.168.3.14[12]
前提条件
- 構築/Linux サーバ/CentOS 5.4で作成した仮想マシンを複製(on VMware Server / on VMware ESXi)して利用する。
1 squid のインストール
squidをさっくりインストール
[root@Sulu ~]# yum install -y squid
2010/03/20時点でインストールしたversion
[root@Sulu ~]# rpm -q squid squid-2.6.STABLE21-3.el5
squid.confの設定
- squid.confを編集
[root@Sulu ~]# /etc/squid/squid.conf
- http_accessの値を設定
#http_access allow localhost #http_access deny all http_access allow all
- http_portの値を設定
vhostオプションを付加しないと http accelerator mode にならない。(Version 2.6から)
http_port 80 vhost
- cache_peerの値を設定
- parentがバックエンドのwebサーバ(apache/192.168.3.12[12])
- siblingはロードバランスするための兄弟squidサーバ(92.168.3.14[12])
- 自身もしくは兄弟にキャッシュがなければ親サーバ(ここではapache/192.168.3.12[12])にround-robin(順番)で問い合わせするという設定
cache_peer 192.168.3.121 parent 80 0 no-query originserver round-robin cache_peer 192.168.3.122 parent 80 0 no-query originserver round-robin cache_peer 192.168.3.141 sibling 80 3130 round-robin cache_peer 192.168.3.142 sibling 80 3130 round-robin
cache_dirの値を設定
キャッシュを保存するディレクトリの設定。
cossがお勧めとはてなの人がいっとるので。
cache_dir coss /var/squid/coss 8000 block-size=512 max-size=524288
- クエリさえもキャッシュするように設定
- pukiwikiもそうだが、QUERY STRINGによってページがことなるので、これもキャッシュしてしまう
キャッシュするファイルについてはproxy_balancerで指定する計画なので、ここではリクエストがあったもの全てをキャッシュする方針
- pukiwikiもそうだが、QUERY STRINGによってページがことなるので、これもキャッシュしてしまう
#acl QUERY urlpath_regex cgi-bin \? #cache deny QUERY
- refresh_patternの値を設定
- ignore-no-cache ~ の記述は動的コンテンツだろうが、キャッシュしないでとwebサーバが言おうが、無視してキャッシュする設定
キャッシュするファイルについてはproxy_balancerで指定する計画なので、ここではリクエストがあったもの全てをキャッシュする方針
- ignore-no-cache ~ の記述は動的コンテンツだろうが、キャッシュしないでとwebサーバが言おうが、無視してキャッシュする設定
#refresh_pattern ^ftp: 1440 20% 10080 #refresh_pattern ^gopher: 1440 0% 1440 #refresh_pattern . 0 20% 4320 refresh_pattern . 1440 20% 4320 ignore-no-cache override-expire ignore-private override-lastmod ignore-reload
- visible_hostnameにホスト名を設定
- ホスト名を設定しないとエラーになるらしい
visible_hostname Sulu-squid01
- keepalive off を設定
client_persistent_connections off server_persistent_connections off
キャッシュの初期化
[root@Sulu ~]# mkdir -p /var/squid/coss [root@Sulu ~]# squid -z
ネットワーク設定
NICの設定
[root@Sulu ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 BOOTPROTO=none HWADDR=00:0c:29:ed:40:e9 ONBOOT=yes DHCP_HOSTNAME=Sulu IPADDR=192.168.3.141 NETMASK=255.255.255.0 GATEWAY=192.168.3.1 TYPE=Ethernet
DNSサーバの設定
[root@Sulu ~]# vi /etc/resolv.conf nameserver 192.168.3.1
ネットワークの再起動
[root@Sulu ~]# service network restart
サービスの開始
[root@Sulu ~]# chkconfig --level 3 iptables on [root@Sulu ~]# service squid start
Appendix
- ViewVCでデフォルトとの差分を参照できます
- squidのキャッシュの削除方法 (2回目のコマンドで404となり、キャッシュが無くなったことを確認しています)
[root@psq01 ~]# squidclient -p 80 -m PURGE http://sugoiyo.com/ HTTP/1.0 200 OK Server: squid/2.6.STABLE21 Date: Wed, 29 Sep 2010 12:51:18 GMT Content-Length: 0 [root@psq01 ~]# squidclient -p 80 -m PURGE http://sugoiyo.com/ HTTP/1.0 404 Not Found Server: squid/2.6.STABLE21 Date: Wed, 29 Sep 2010 12:52:05 GMT Content-Length: 0
- PURGE(削除というか解体?)するためにはconfig中の以下記述がキモとなります。
acl PURGE method PURGE http_access allow PURGE localhost
- squid.confのdiff -u
@@ -633,8 +633,9 @@ #http_access allow our_networks # And finally deny all other access to this proxy -http_access allow localhost -http_access deny all +#http_access allow localhost +#http_access deny all +http_access allow all # TAG: http_access2 # Allowing or Denying access based on defined access lists @@ -916,7 +917,7 @@ # visible on the internal address. # # Squid normally listens to port 3128 -http_port 3128 +http_port 80 vhost # TAG: https_port # Usage: [ip:]port cert=certificate.pem [key=key.pem] [options...] @@ -1447,6 +1448,11 @@ #Default: # none +cache_peer 192.168.3.121 parent 80 0 no-query originserver round-robin +cache_peer 192.168.3.122 parent 80 0 no-query originserver round-robin +cache_peer 192.168.3.141 sibling 80 3130 round-robin +cache_peer 192.168.3.142 sibling 80 3130 round-robin + # TAG: cache_peer_domain # Use to limit the domains for which a neighbor cache will be # queried. Usage: @@ -1780,6 +1786,7 @@ # #Default: # cache_dir ufs /var/spool/squid 100 16 256 +cache_dir coss /var/squid/coss 8000 block-size=512 max-size=524288 # TAG: store_dir_select_algorithm # Set this to 'round-robin' as an alternative. @@ -2366,8 +2373,8 @@ # # Default is to allow all to be cached #We recommend you to use the following two lines. -acl QUERY urlpath_regex cgi-bin \? -cache deny QUERY +#acl QUERY urlpath_regex cgi-bin \? +#cache deny QUERY # TAG: refresh_pattern # usage: refresh_pattern [-i] regex min percent max [options] @@ -2448,9 +2455,10 @@ # used. # #Suggested default: -refresh_pattern ^ftp: 1440 20% 10080 -refresh_pattern ^gopher: 1440 0% 1440 -refresh_pattern . 0 20% 4320 +#refresh_pattern ^ftp: 1440 20% 10080 +#refresh_pattern ^gopher: 1440 0% 1440 +#refresh_pattern . 0 20% 4320 +refresh_pattern . 1440 20% 4320 ignore-no-cache override-expire ignore-private override-lastmod ignore-reload # TAG: quick_abort_min (KB) # TAG: quick_abort_max (KB) @@ -3000,6 +3008,7 @@ # #Default: # none +visible_hostname Sulu-squid01 # TAG: unique_hostname # If you want to have multiple machines with the same @@ -3402,6 +3411,8 @@ #Default: # client_persistent_connections on # server_persistent_connections on +client_persistent_connections off +server_persistent_connections off # TAG: persistent_connection_after_error # With this directive the use of persistent connections after @@ -3677,6 +3688,7 @@ # #Default: # icp_query_timeout 0 +icp_query_timeout 2000 # TAG: maximum_icp_query_timeout (msec) # Normally the ICP query timeout is determined dynamically. But
Version2.6 - http accelerator mode ( コンテンツキャッシュサーバ )
