それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

squid をcygwin インストール

Squidを見なおしてずっと活用しています。便利よね。一家に一台プロキシサーバー


f:id:takuya_1st:20130516005940j:plain

Squidのメリット。

  • 速い
  • 簡単
  • 分散
  • 枯れてる

mod_proxy と比較した時、squid の方がApacheと切り離して管理できるので、放置する文意はまぁ楽かもしれません。

Squidは分散キャッシュを使えるので複数台運用にはもってこいですね。あとリバースプロキシとしても大活躍中です。Windowsでキャッシュサーバーを構築するメリットはあまりないのですが。Windowsマシンをつけっ放して置いておくなら、電気代を有効活用するためにSquidサーバーとして使うことにしました。Cygwinでやっているのはもはや趣味のレベルですが。

また、LTEの7GB制限を回避するためにキャッシュと圧縮をやるのに役立ちそうです。
Opera Turboでもいいんだけどね

CygwinのデフォルトのSquid

インストール

apt-cyg install squid
/usr/sbin/squid
#...無応答

動かない

SquidCygwinに用意されていると、ググったら出てきたので、インストールしたら動かない。。。仕方ないのでコンパイルをしました。

CygwinSquidコンパイル

  1. Squid公式ドキュメントを参考に
  2. Squidソースコードを持ってきて
  3. コンパイルして使う

というプランです。結果から言うとSquid3系は簡単にコンパイルできるというわけでは無さそうでした。Squid2系は、TarBallをコンパイルしたら、直ぐに動きました。

コンパイル方法を参考に

コンパイル方法はSquidの公式Wikiに書いてあります

Cygwin (Windows)

In order to compile Squid, you need to have Cygwin fully installed.

The usage of the Cygwin environment is very similar to other Unix/Linux environments, and -devel version of libraries must be installed.

WCCP is not available on Windows so the following configure options are needed to disable them:

  --disable-wccp
  --disable-wccpv2


Squid will by default, install into /usr/local/squid. If you wish to install somewhere else, see the --prefix option for configure.


Now, add a new Cygwin user - see the Cygwin user guide - and map it to SYSTEM, or create a new NT user, and a matching Cygwin user and they become the squid runas users.

Read the squid FAQ on permissions if you are using CYGWIN=ntsec.

After run squid -z. If that succeeds, try squid -N -D -d1, squid should start. Check that there are no errors. If everything looks good, try browsing through squid.

Now, configure cygrunsrv to run Squid as a service as the chosen username. You may need to check permissions here.

http://wiki.squid-cache.org/SquidFaq/CompilingSquid

Squidのビルド

  • squid3.3 
    • make 失敗
  • squid.2.7 stable
    • make 失敗

単純に、 ./congire では動かない。。。

--disable-internal-dns を付けてうまくいきました

./configure --disable-wccp --disable-wccpv2 --disable-internal-dns
make
make install

でうまくいきました。

make install 結果

/usr/local/squid にインストールされました

インストールされた結果は、こんな感じのディレクトリ構造になりました。

takuya@letsnote:~$ tree /usr/local/squid/ -L 2
/usr/local/squid/
├── bin
│   ├── cossdump.exe
│   ├── RunCache
│   └── squidclient.exe
├── etc
│   ├── cachemgr.conf
│   ├── mime.conf
│   ├── mime.conf.default
│   ├── squid.conf
│   ├── squid.conf.default
│   └── squid.htpasswd
├── libexec
│   ├── cachemgr.exe
│   ├── dnsserver.exe
│   ├── ncsa_auth.exe
│   └── unlinkd.exe
├── sbin
│   └── squid.exe
├── share
│   ├── errors
│   ├── icons
│   ├── man
│   └── mib.txt
└── var
    ├── cache
    └── logs

Squidを設定して動くようにします。

/usr/local/squid の中で作業します。Cygwinの空間と被るのがちょっといやなので /usr/local のインストール空間でよしとしました。


とりあえず プロキシとして動作させる。

設定をして、まずはフォワードプロキシとして動作確認

/usr/loca/squid/etc/squid.conf
http_access allow all
visible_hostname  xxx.193.9.10

とりあえず、まずは全てイケイケ系のオープンプロキシ(設定が単純)で動作確認をします。
設定ができたら、起動します。

開始と停止を覚えてチェックする

init スクリプトによる自動起動が用意できないので手作業で起動です。
もし自動起動するなら、CygwinではサービスはCygserver経由ですしね。

起動
/usr/local/squid/sbin/squid
終了
/usr/local/squid/sbin/squid -k shutdown 

ローカルホストからチェックする

早速チェックしましょう。今回は、グローバルIPにプロキシを置いたので確認君を遣いました。
ローカル環境なら、ローカル環境に phpinfo()のApacheサーバーにアクセスして、ホスト名が変わっているかProxy Via が付いたことを確認します。

確認くんでアクセス

squidが通常のアクセスの場合

まずは、Squidコマンドで、Squidが正しく通信できることを確認します。

/usr/local/squid/bin/squidclient.exe  http://www.ugtop.com/spill.shtml | nkf -w
プロキシ経由の場合

つぎに、CurlコマンドがSquid経由で正しく通信できることを確認します

http_proxy=http://192.168.95.34:3128 curl http://www.ugtop.com/spill.shtml

2つでアクセスし、その結果にあるHTTP_REMOTE_ADDRがSuqidのIPになっていれば成功です。

アクセス制限を掛ける。

次に、アクセス制限を掛けます。このままでは、Squidはオープンプロキシなので各方面に面倒を掛ける転送屋さんになっています。悪用されて、謂れ無き非難を浴びるのは嫌なので、アクセス制限をかけましょう。

アクセス制限のモジュールをコンパイルします

最小インストールでははいってなかったので、追加する。
./configure --disable-wccp --disable-wccpv2 --disable-internal-dns --enable-auth=basic --enable-basic-auth-helpers=NCSA 
make 
make install 

ちょっと使うだけなので、簡単なNCSAにしました。
ApacheBasic認証と同じ方式です。安全とはいえませんが、乱用を防止するには少しくらいなら役に立ちそうです。

アクセス制限設定

squid.confで次のように設定します。

#http_access allow all
acl password proxy_auth REQUIRED
http_access allow password  


auth_param basic program /usr/local/squid/libexec/ncsa_auth /usr/local/squid/etc/squid.htpasswd
auth_param basic children 5
auth_param basic realm Squid proxy-caching web server
#auth_param basic concurrency 0

パスワードファイルを作ります。

htpasswd -c /usr/local/squid/etc/squid.htpasswd takuya_1st

でパスワードファイルを作ります。

digest でもbasic でもいいのでとりあえずパスワードを掛けておけば安心ですね。

Squid起動しました

バージョン2.6だけど、動かすにはこれで充分だよね。。。

f:id:takuya_1st:20130516005937p:plain