トップ   サーバー構築   旅行日記   手記   リンク   About  

squid

squidに要求されるスペック

squidを動かす上で重要なのはメモリとディスクの容量と書き込みスピードです。
CPUスペックはそれほど重要ではありません。
メモリは512MB以上
10G以上でなるべく回転数とアクセス速度が高いことが望ましいです。

インストール

tar xzf squid-2.5.STABLE9.tar.gz
cd squid-2.5.STABLE9
./configure --enable-err-language=Japanese
make
make install

squid実行ユーザー登録

groupadd -g 503 squid
useradd -g squid -u 503 squid
IDは503で登録します。

キャッシュディレクトリの作成

mkdir /var/spool/squid
chown squid:squid /var/spool/squid

squid.confの編集

vi /usr/local/squid/etc/squid.conf

HTTPポートの設定

#Default: 
http_port 3128 

プロキシサーバーからプロキシへ接続する場合

# TAG: cache_peer 
#Default: 
# none 
もし追加する場合は none のコメントをはずし以下の用に入力する
例:cache_peer 他プロキシサーバ名 parent 3128 3130 no-query
# TAG: dead_peer_timeout (seconds) 
#Default: 
# dead_peer_timeout 10 seconds 
(dead_peer_timeout 20 seconds )
※プロキシからプロキシへの接続を行わない場合は省略する。

キャッシュメモリ

指定しない場合は省略する。
# TAG: cache_mem 
#Default: 
cache_mem 8 MB

キャッシュディレクトリの設定

#Default: 
cache_dir ufs /usr/local/squid/var/cache 100 16 256 
Cacheフォルダの場所指定
キャッシュ容量
サブフォルダ数
更にサブフォルダ数
の順番で記述します。

キャッシュのアクセスログ

# TAG: cache_access_log 
#Default: 
cache_access_log /usr/local/squid/var/logs/access.log
デフォルトではlogsになっているが、フォルダはvar下になる。

キャッシュのログ

# TAG: cache_log 
#Default: 
cache_log /usr/local/squid/var/logs/cache.log

Apacheのログフォーマットに設定

#Default: 
emulate_httpd_log on
(略) 
デフォルトはoffである。

プロセスIDの指定

# TAG: pid_filename 
#Default: 
# pid_filename /usr/local/squid/logs/squid.pid
# TAG: ftp_user 
#Default: 
ftp_user Squid@domain.name
anonymousFTPでのログイン時のPassをして使うメールアドレス

DNSを参照する子プロセス数

# TAG: dns_children 
#Default: 
# dns_children 5 
パフォーマンスが低い時にこの数値を上げる。

参照DNSの設定

# TAG: dns_nameservers 
dns_nameservers 192.168.1.10 

パラメータファイルの指定

acl clients01 src “/usr/local/squid/etc/permit.txt”
端末のIPアドレスをpermit.txtに指定
acl denyurl dstdomain ”/usr/local/squid/etc/url.txt”
Webリクエストを拒否するリストをurl.txtに指定

指定URLのアクセス拒否の設定

# TAG: http_access 
http_access deny denyurl
url.txt で定義したURLへの接続を拒否します。
http_access allow clients
http_access allow clients01    
aclで宣言したclientの接続を許可

Logファイルの設定

# TAG: logfile_rotate 
#Default: 
# logfile_rotate 10
Logファイルの保存個数

LogにクライアントのIPを残すか

# TAG: forwarded_for on|off 
#Default: 
# forwarded_for on 
forwarded_for off
on = 残す、off = 残さない

Proxy接続許可端末の定義

/usr/local/squid/etc/permit.txt を編集します。
192.168.1.11 
192.168.2.23 
192.168.3.33 
上記は設定例

Webリクエスト拒否サイトの定義

/usr/local/squid/etc/url.txt を編集します。
.xxx.xxx.com 
.yyy.net 
.zzz.co.jp 
…etc 
上記のように入力します。

Squidの初期化

キャッシュフォルダのパーミッションの変更

初期化すると squid よりファイルが書き込まれる。しかしデフォルトだと書き込み権限がないのでエラーではじかれてしまいます。
そのため、squid が書き込むフォルダの権限を変更する必要があります。
chown squid.squid /usr/local/squid/var/cache/
chmod -R 770 /usr/local/squid/var/cache/
初期化コマンド実行
/usr/local/squid/bin/squid -f /usr/local/squid/etc/squid.conf -z 
上記コマンドを実行します。
Creating Swap Directories と出ればOK

Squidの起動

起動させる前に起動スクリプトをセットします。
ソースファイルの以下のパスに起動スクリプトがあります。
cp contrib/squid.rc /etc/init.d/squid
chmod 755 /etc/init.d/squid
起動シェルの修正
#!/sbin/sh ==> #!/bin/sh 
/etc/init.d/squid を編集して3行目以降に以下の行を追加します。
# chkconfig: 345 90 42
# description: squid is Proxy server.

chkconfig への登録

chkconfig --add squid
chkconfig squid on

起動

chkconfig squid start
※squidがDNS検索を出来ないと起動できません。

ログの整理

#/usr/local/squid/bin/squid -k rotate 
上記コマンドで、Logを別名保存してリフレッシュする。

自動化する場合

cronに登録します。
0 0 1 * * /usr/local/squid/bin/squid -k rotate 
毎月1日0時に処理実行

Logの時刻表示について

squidの access.log 内の時刻はデフォルトだと、1970年からの通算のカウント数になるので、時刻を標準時刻に変換して表示させる。
/usr/local/bin/henkan.pl ファイルを編集する。
[/usr/local/bin/henkan.pl] 
#! /usr/bin/perl -p 
s/^\d+\.\d+/localtime $&/e; 

チェンジモードを実行

chmod 755 henkan.pl 
perl /usr/local/bin/henkan.pl access.log > access.log.a 
※access.log.aに標準時刻に変換されたlogが出力される。

高度な設定

より squid を細かく設定したい場合に以下の設定を記します。

プログラム名及びバージョンの隠蔽

もしバージョン情報を表示したくない場合はコンパイルする前に、configureファイルを編集する。
[configure] 
# Define the identity of the package. 
#PACKAGE=squid 
PACKAGE=xxx 
#VERSION=2.5.STABLE2 
VERSION=yyy 
※デフォルトの情報をコメントし適当な文字列で定義する

透過Proxyとして稼動させたい

/usr/local/squid/etc/squid.conf を編集します。
httpd_accel_host virtual
httpd_accel_port 80,443
httpd_accel_with_proxy on
httpd_accel_uses_host_header on
httpd_accel_single_host off
※上記の5行を追加

iptablesの設定
iptables -t nat -A PREROUTING -p tcp \
 --dport 80 -j REDIRECT --to-port 8080 
※Potr:80で通過するプロトコルを全てローカルのPort:8080へリダイレクト
※透過するPortは 80のみのよう。他のPortの定義も行なったが無理だった。(https,ftp等)

パスワード認証をさせたい

パスワード認証をさせる場合は ncsa_auth をインストールさせる必要があります。 ※squid をまだインストールしていない場合
tar -zxvf squid-2.5.STABLE2.tar.gz 
cd squid-2.5.STABLE2/ 
./configure --enable-auth=basic \
  --enable-basic-auth-helpers="NCSA" 
make 
make install 
※既にsquidインストール済みの場合
cd squid-2.5.STABLE2/ 
cd helpers/basic_auth/NCSA/ 
make 
make install 
※ncsa_authファイルは /usr/local/squid/libexec/ 以下にインストールされます。

パスワードファイルを作成

パスワードファイルを生成するには、htpasswd が必要です。
このプログラムは、apacheに含まれています。
/usr/local/apache2/bin/htpasswd -c \
    /usr/local/squid/etc/passwd haya 
New password: 
Re-type new password: 
Adding password for user testuser 
/usr/local/squid/etc/passwd と言うファイルを作成し"testuser"というユーザーアカウントを追加

squid.confの編集

squid.confの中に以下の行を追加 [/usr/local/squid/etc/squid.conf] auth_param basic program /usr/local/squid/libexec/ncsa_auth /usr/local/squid/etc/passwd acl password proxy_auth REQUIRED http_access allow password ※上2行はコメントを外して、パスを確認するだけでよい。 設定完了後、squidを再起動 (5)Proxt to Proxy接続をさせたい 以下のように squid.conf を設定する cache_peer 192.168.100.1 parent 8080 3130 no-query ↑親Proxyの定義 acl local-external dstdomain www.haya.local always_direct deny local-external ↑www.haya.localへは、子Proxyから直接接続させない acl local-intranet1 dstdomain .haya.local acl local-intranet2 dst 192.168.100.0/24 always_direct allow local-intranet1 always_direct allow local-intranet2 ↑".haya.local"及び"192.168.100.0/24"は子Proxyから直接接続させる。 never_direct allow all ↑必ず親Proxyを経由させる (6)接続情報を制限したい バージョンの隠蔽 #vi configure [configure] # Define the identity of the package. PACKAGE=unknown VERSION=unknown ※以上のように編集してからコンパイルを行なう。 各種パラメーター [squid.confの編集] visible_hostname unknown ・・・・・・ProxyServerのホスト名の偽装 forwarded_for off ・・・・・・・・・端末PCのIPを出さない header_access Via deny all ・・・・・Proxyのバージョン、ホスト名を出さない header_access User-Agent deny all ・・ブラウザの情報を出さない(WindowsUpdateが出来なくなる)
Copyight (C) qurataro.com 2004-2007 quraportal All Rights. Reserved.