自宅サーバー(Mac mini)にリバースProxyを入れようとして挫けた話
多分できたとは思うのですけど・・
この自宅のWebサーバー(Mac mini G4, Mac OS X 10.4.11 Tiger)ですが、高速化とセキュリティ対策のためにSquidをリバースProxyとして導入してみようと思って色々やってみましたが、結局挫けました(;_;)。何故かと言うと、多分SquidをリバースProxyとして動作させる事はできたと思うのですが、そうした途端このBLogに記事を書き込む、あるいは管理者としてログインすることができなくなってしまったのです。この問題を突破することができず、結局Squidは普通のフォワードProxyとして動作させています。自分の技術レベルの低さを改めて認識。
でも折角Squidを入れたのですから、備忘録としてSquidの導入方法を記しておこうと思います。
1. Squidの入手
Squidはsquid-cache.orgのdownloadページからダウンロードします。現時点でのstable最新版はVer.3.5.2のようですが、これをビルドしようとしてもダメでした。どうやらSASL(Simple Authentication and Security Layer)のヘッダファイルが必要らしいのですが、MacOS X 10.4.11 TigerにSASL自体は入っているもののヘッダファイルは入っていないようです。と言うわけで、古いVersionのSquidを使いました。
Ver.3.2系の最終版squid-3.2.13-20130721-r11827ではビルドはできるものの、動かすと異常終了してしまいましたが、Ver.3.1系の最終版squid-3.1.23-20130721-r10487ではビルド、動作ともに問題なさそうでした。と言うわけで、少し古いですがこのsquid-3.1.23-20130721-r10487を使うことにします。
2. Squid Ver.3.1.23のビルド
squid-3.1.23-20130721-r10487.tar.bz2をダウンロードし、以下の手順でビルド、インストールしました。
$ bzcat squid-3.1.23-20130721-r10487.tar.bz2 | tar xvf -
$ cd squid-3.1.23-20130721-r10487
$ ./configure --prefix=/usr/local/squid --with-large-files
$ make
$ sudo make install
|
上記で Squidが /usr/local/squid配下にインストールされます。ポイントは2GBを越えるファイルも扱えるよう --with-large-files を指定しておいたところ位かな。
3. squidユーザー、グループの追加
Squid専用のユーザーとグループを追加します。Linux等ではrootで useradd, groupaddコマンドを使いますが、MacOS X 10.4.11 Tigerでは"アプリケーション/ユーティリティ/NetInfo マネージャ"を使います。
1) squidグループの追加
2) squidユーザーの追加
4. インストールした /usr/local/squid のowner, groupをsquidに変更
$ cd /usr/local
$ sudo chown -R squid:squid ./squid
|
5. squid.confファイルの編集
squidの設定ファイル"/usr/local/squid/squid.conf" を任意のエディタで編集します。
$ cd /usr/local/squid/etc
$ sudo cp -p squid.conf.default squid.conf
$ sudo vi squid.conf
|
フォワードProxyとして以下のように設定しました。変更箇所は赤字の部分です。
#
# Recommended minimum configuration:
#
acl manager proto cache_object
acl localhost src 127.0.0.1/32 ::1
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1
# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7 # RFC 4193 local private network range
acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines
acl SSL_ports port 443
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
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 CONNECT method CONNECT
#
# Recommended minimum Access Permission configuration:
#
# Only allow cachemgr access from localhost
http_access allow manager localhost
http_access deny manager
# Deny requests to certain unsafe ports
http_access deny !Safe_ports
# Deny CONNECT to other than secure SSL ports
http_access deny CONNECT !SSL_ports
# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
#http_access deny to_localhost
#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#
# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
http_access allow localnet
http_access allow localhost
# And finally deny all other access to this proxy
http_access deny all
# Squid normally listens to port 3128
http_port 8080
cache_mem 16 MB
# Uncomment and adjust the following to add a disk cache directory.
cache_dir ufs /usr/local/squid/var/cache 200 32 256
# Leave coredumps in the first cache dir
coredump_dir /usr/local/squid/var/cache
# Add any of your own refresh_pattern entries above these.
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|?) 0 0% 0
refresh_pattern . 0 20% 4320
cache_effective_user squid
cache_effective_group squid
#
# invisible setup
#
visible_hostname unknown
forwarded_for off
request_header_access From deny all
request_header_access Referer deny all
request_header_access User-Agent deny all
request_header_access X-Forwarded-For deny all
request_header_access Via deny all
request_header_access Cache-Control deny all
reply_header_access X-Forwarded-For deny all
reply_header_access Via deny all
reply_header_access Cache-Control deny all
|
6. squid.confファイルのチェック
編集した squid.conf に問題がないかチェックします。
$ sudo /usr/local/squid/sbin/squid -k parse
|
7. squidのキャッシュディレクトリ作成
始めてSquidを実行する前に、設定ファイルの cache_dirで指定したキャッシュディレクトリ(今回の場合は "/usr/local/squid/var/cache")を作成しておきます。
$ sudo /usr/local/squid/sbin/squid -z
|
これは cache_dirの設定を変更した場合や、キャッシュを初期化したい場合にも行います。
8. squid起動スクリプトの作成
squidがブート時に自動起動するよう、/Library/LaunchDaemons配下にスクリプトを作成します。
$ cd /Library/LaunchDaemons
$ vi org.squid.plist
|
スクリプトは以下のようにしました。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.squid</string>
<key>OnDemand</key>
<false/>
<key>ProgramArguments</key>
<array>
<string>/usr/local/squid/sbin/squid</string>
<string>-N</string>
<string>-s</string>
<string>-d 1</string>
<string>-D</string>
</array>
<key>ServiceIPC</key>
<false/>
</dict>
</plist>
|
注意する点は -Nオプションを指定すること。-Nオプションはデーモンプロセスとして起動しないようにするオプションです。
OnDemand が false の場合 LaunchDaemons は起動状態を維持するような動作になるらしく、squidがデーモンとして常駐して終了すると LaunchDaemonsが再び squidを起動させます。そのため、-Nオプションを指定しなかった時は無限にsquidプロセスが増殖していくという現象が起きて、えらい目にあってしまいました。
9. squidを起動させる
launchctlコマンドを使用してsquidを起動させます。lauchctl に loadを指定する場合、ファイルはフルパスで指定する必要がある事に注意。また -w オプションを指定して load しているので、ブート時に自動起動するようになります。
$ sudo launchctl load -w /Library/LaunchDaemons/org.squid.plist
|
起動したか確認してみます。
$ sudo launchctl list
〜
org.squid
$ ps -aux | grep squid
squid 16949 0.0 0.8 60276 7968 ?? Ss Mon09PM 2:45.98 /usr/local/squid/sbin/squid -N -s -d 1 -D
squid 16950 0.0 0.1 27252 1284 ?? Ss Mon09PM 0:00.03 (unlinkd)
|
無事に起動しているようです。
squid.confの設定を変更した際に squidを再起動するには、
$ sudo launchctl stop org.squid
|
とします。stop はジョブを停止させるのですが、OnDemand が false なので停止後すぐに起動し、つまり再起動になります。本当に squidを停止させたいときは、
$ sudo launchctl unload /Library/LaunchDaemons/org.squid.plist
|
です。LaunchDaemons(launchctl)の使い方は少しクセがあるみたいで、あちこちのサイトを参考にさせていただきました。こちらなどが良くまとまっているかと思います。
|