SquidによるProxyサーバの構築


 Proxy(代理)サーバはセキュリティ強化(ファイアーウォール)のために利用されるが,積極的な意味では,データを備蓄してそれを再利用することにも応用される。その目的に利用するためのProxyサーバのことをキャッシュサーバと呼んでいる。

 クライアントマシンで,Webページを閲覧した場合,そのデータはそのクライアントのハードディスクの中にキャッシングされるが,このときにデータが通過したProxyサーバ内にもキャッシングすれば,別のクライアントで同じWebページを閲覧しようとしたとき,いちいちインターネットからデータを持ってくるのではなく,Proxyサーバにキャッシュされたデータを持ってくれば,その分読み込みは高速となり,外部への回線のトラフィックの軽減にもなる。例えば,授業などで利用するサイトをあらかじめ閲覧し,キャッシュしておくことにより細い回線でも生徒は快適に閲覧することができるようになる。

 さらに,外部ネットワークと内部ネットワークを分離するゲートウェイにProxyサーバを設置することによりセキュリティ対策にもなる。

●Proxyサーバ「Squid」

 Proxyサーバに多くの種類があるが,本研修では,UNIXで動作し,キャッシュサーバに機能に特化した「Squid」を使用する。UNIXで動作する他のProxyサーバとしてはApacheのProxyやCERN HTTPDやDelegateなどある。

(1)ネットワーク構成

 Proxyサーバを設置する位置によりネットワークの構成が変わる。基本的には,Proxyサーバ単独で利用する場合と,ゲートウェイを兼ねる場合である。以下にネットワーク構成図を示す。

linux_proxy1.gif (5060 バイト)

 既存のネットワークに,端末と同じようにProxyサーバを追加設置するため,ブラウザの設定にProxyサーバを指定するだけでよい。 


 linux_proxy2.gif (4241 バイト)

 Proxyサーバをゲートウェイに設置した場合,外部向けのパケットは全てProxyサーバを経由しなければならない。このため,Proxyサーバが中継しないプロトコルでの通信は行えない。Squidの場合,中継するプロトコルはHTTP等なので,Web以外のインターネット利用は出来なくなる。ファイアウォールとしては強固である。


 linux_proxy3.gif (4697 バイト)


  IPマスカレードを利用したファイアウォールにProxyサーバ機能を組み込む方法である。Proxyサーバが中継しないプロトコルはIPマスカレードによって通すことにより,他のインターネットプロトコルも使用できるようにし,Proxyサーバはキャッシュサーバとして利用する。

 

●構築するネットワーク構成

 構築するネットワーク構成を,以下の図に示す。IPマスカレードファイアウォールに,Proxyサーバを加えた構成である。

linux_proxy4.gif (6123 バイト)


(2)Squidのインストール

 Linuxディストリビューションは,Laser5 Linux6.0を使用する。Squidがインストールされていない場合は,Laser5 Linux6.0のCD-ROMよりgnoRPMやRPMコマンドにより,Squid RPMパッケージを導入する。Laser5 Linux6.0付属のSquidパッケージは「squid-2.2.STABLE4-5.i386.rpm」である。


(3)Squidの基本設定

 Squidの設定ファイルは,/etc/squid/squid.confである。squid.confは約1900行もあるテキストファイルであり,設定する項目は無数あるが,設定するべき項目は少ない。

 エディタで,/etc/squid/squid.confを開き,次に示す   個所を変更する。

/etc/squid/squid.conf

<省略>約19行目

# NETWORK OPTIONS
# -----------------------------------------------------------------------------
# TAG: http_port
# The port number where Squid will listen for HTTP client
# requests. Default is 3128, for httpd-accel mode use port 80.
# May be overridden with -a on the command line.
#
# You may specify multiple ports here, but they MUST all be on
# a single line.
#
#http_port 3128
プロキシーのポート番号を指定,デフォルトでは3128
http_port 8080

# TAG: icp_port
# The port number where Squid sends and receives ICP requests to
# and from neighbor caches. Default is 3130. To disable use
# "0". May be overridden with -u on the command line.
#
#icp_port 3130
# TAG: htcp_port
# The port number where Squid sends and receives ICP requests to
# and from neighbor caches. Default is 4827. To disable use
# "0".
#
# To enable this option, you must use --enable-htcp with the
# configure script.
#htcp_port 4827

<省略>約190行目

#
# NOTE: non-ICP neighbors must be specified as 'parent'.
#
#cache_peer hostname type 3128 3130

<省略>約345行目

# The default is 8 Megabytes.
#

キャッシュに使うメモリ量,搭載メモリが豊富ならば増やせばよいが,デフォルトでかまわない

cache_mem 8 MB

# TAG: cache_swap_low (percent, 0-100)
# TAG: cache_swap_high (percent, 0-100)
# The low- and high-water marks for cache LRU replacement. LRU
# replacement begins when the high-water mark is reached and ends
# when enough objects have been removed and the low-water mark is
# reached. Defaults are 90% and 95%. If you have a large cache, 5%
# could be hundreds of MB. If this is the case you may wish to
# set these numbers closer together.
#
#cache_swap_low 90
#cache_swap_high 95
# TAG: maximum_object_size (bytes)
# Objects larger than this size will NOT be saved on disk. The
# value is specified in kilobytes, and the default is 4MB. If
# you wish to get a high BYTES hit ratio, you should probably
# increase this (one 32 MB object hit counts for 3200 10KB
# hits). If you wish to increase speed more than your want to
# save bandwidth you should leave this low.
#

どれくらいのサイズのファイルまでキャッシュするの指定。デフォルトは4MBである。

特に変更は不要

maximum_object_size 4096 KB

<省略>約730行目

# TAG: reference_age
# As a part of normal operation, Squid performs Least Recently
# Used removal of cached objects. The LRU age for removal is
# computed dynamically, based on the amount of disk space in
# use. The dynamic value can be seen in the Cache Manager 'info'
# output.
#
# The 'reference_age' parameter defines the maximum LRU age. For
# example, setting reference_age to '1 week' will cause objects
# to be removed if they have not been accessed for a week or
# more. The default value is one month.
#
# Specify a number here, followed by units of time. For example:
# 1 week
# 3.5 days
# 4 months
# 2.2 hours
#

どれくらいの期間キャッシュを持ち続けるかの設定。デフォルトは1ヶ月,3 daysとかでよいかも

reference_age 1 month

<省略>約990行目

#Examples:
#acl myexample dst_as 1241
#acl password proxy_auth REQUIRED
#
#Defaults:
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 SSL_ports port 443 563
acl Safe_ports port 80 21 443 563 70 210 1025-65535
acl CONNECT method CONNECT

proxyサーバを利用できるホストを定義,

192.168.1.xのIPアドレスのクライアントをlocalnetとして定義している

acl localnet src 192.168.1.0/255.255.255.0

# TAG: http_access
# Allowing or Denying access based on defined access lists
#
# Access to the HTTP port:
# http_access allow|deny [!]aclname ...
#
# Access to the ICP port:
# icp_access allow|deny [!]aclname ...
#
# NOTE on default values:
#
# If there are no "access" lines present, the default is to allow
# the request.
#
# If none of the "access" lines cause a match, the default is the
# opposite of the last line in the list. If the last line was
# deny, then the default is allow. Conversely, if the last line
# is allow, the default will be deny. For these reasons, it is a
# good idea to have an "deny all" or "allow all" entry at the end
# of your access lists to avoid potential confusion.
#
#Default configuration:
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#
http_access allow localhost
Proxyを利用できるものとして,前述のacl名であるlocalnetを許可する

http_access allow localnet

http_access deny all

 あとは,運用をしながらチューニングを施す。設定の詳細については「http://squid.nlanr.net/」を参考にしてもらいたい。


(4)Squidの起動と停止

 Squidの起動は,ターミナルよりルート権限で,

# cd /etc/rc.d/init.d
#./squid start

と入力し,起動せさる。

 エラーなど発生し,起動できないときはsquid.confを見直す。

Squidの起動中に,squid.confを変更した場合は,

# cd /etc/rc.d/init.d
#./squid restart

とする。

 Squidをサービスとして起動する場合は,ntsysvコマンドを利用する。

#ntsysv

と入力し,「squid」の項目の前に「*」を付ける。

 この操作により,システムを起動するれば,squidはサービスとして自動起動する。

Squidを停止したい場合は,

# cd /etc/rc.d/init.d
#./squid stop

でよい。


(5)ブラウザのProxy設定

 InternetExplorer4.01の例を示す。

 InternetExplorer4.01のメニューより「表示(V)」→「オプション(O)」を選択し,「インターネットのプロパティ」画面より「接続」タブを表示させ,「プロキーサーバー」の項で,詳細(V)を選択する。

linux_proxy5.gif (18929 バイト)

「プロキシー設定」画面において,「HTTP(H)」,「FTP(F)」の項に,構築した,プロキシーサーバのIPアドレスを指定し,ポート番号は,「Squid.conf」で指定した。「8080」に設定する。


(6)Squidによる有害情報へのアクセス制御

 Squidにより,アクセス制御する方法について説明する。

  1. ブロックするURLリストファイルの作成

 以下に示すリストのように,アクセス制御するURLのリストを正規表現で記入したテキストファイル作成する。

/etc/squid/blacklist.txt

^http://www.yahoo.co.jp/
^http://www.goo.co.jp/

 上記の例では,/etc/squidのディレクトリの中に「blacklist.txt」として,「Yahoo!JAPAN」と「goo」に対してアクセス制御した場合である。

  1. ブロックするパス名リストファイル(URLからホスト名を除いた部分)の作成

     以下に示すリストのように,アクセス制御するパスのリストを正規表現で記入したテキストファイル作成する。

    /etc/squid/blackpath.txt

    xxx
    adult
    sex

     上記の例では,/etc/squidのディレクトリの中に「blackpath.txt」として,「xxx」,「adult」,「sex」の文字列ががURLに含まれる場合に対してアクセス制御する例である。

     

    ※正規表現

     正規表現が,「^」で始まっていれば前方一致(文字列の先頭部分が一致),「$」で終わっていれば後方一致(文字列の最後の部分が一致),「^」と「$」で文字列がはさまれていれば完全一致を意味する。

     

  2. Squid.confの定義

<省略>約990行目

#Examples:
#acl myexample dst_as 1241
#acl password proxy_auth REQUIRED
#
#Defaults:
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 SSL_ports port 443 563
acl Safe_ports port 80 21 443 563 70 210 1025-65535
acl CONNECT method CONNECT
acl localnet src 192.168.1.0/255.255.255.0

acl blacklist url_regex “/etc/squid/blacklist.txt”
acl blackpath urlpath_regex /etc/squid/blackpath.txt

<省略>

#Default configuration:
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#
http_access deny blacklist
http_access deny blackpath

http_access allow localhost
http_access allow localnet
http_access deny all

Squidがすでに起動している場合は,/etc/rc.d/init.d/squid restartとしてやれば,設定が更新される。

※Squidの設定ではまってしまった場所

squid.confの設定で,以下のようにキャッシュサイズ500MBに増や設定をしたところ,Restartがひたすら時間がかかり,うまくいかなかった。

cache_dir /var/spool/squid 500 16 256

最近の,squid.confを見たところ,以下のように「ufs」がついているではないか!

cache_dir ufs /var/spool/squid 100 16 256

「ufs」を付けたところ,あっさり解決...。


参考文献:


HOMEへ


2000 Copyright© T.Kumashiro