ウェブサイト検索

RHEL/CENTOS で SQUID プロキシ サーバーをインストールおよび構成する方法


SQUID プロキシ サーバー

プロキシ サーバーは、通常、クライアントとユーザーが到達しようとしている宛先にあるデバイスです。これにより、プロキシの背後にあるクライアントにセキュリティ、匿名性、さらには保護を提供できます。このプロセスを支援するのが、Red Hat の Web プロキシ サーバーである Squid です。これは、クライアントとユーザーが接続しようとしている Web サーバーの間に位置します。多くの場合、これらのデバイスは、インターネットへのアクセスを制御する場合 (Web フィルタリングを考えてください) に使用されます。 Web プロキシとして、ユーザーが Web から要求したデータをキャッシュしてローカルで利用できるようにすることもでき、ゲートウェイやファイアウォールなどの外部デバイスの負荷を軽減します。

Squid プロキシ サーバーは通常、元のファイルのある Web サーバーとは別のサーバーにインストールされます。 Squid は、ネットワーク上のオブジェクトの使用を追跡することによって機能します。 Squid は最初は仲介者として機能し、単にクライアントのリクエストをサーバーに渡し、リクエストされたオブジェクトのコピーを保存します。 Squid のキャッシュの有効期限が切れる前に、同じクライアントまたは複数のクライアントが同じオブジェクトを要求した場合、Squid はすぐにそのオブジェクトを処理できるため、ダウンロードが高速化され、帯域幅が節約されます。

squid パッケージのインストール:

デフォルトではsquidパッケージはインストールされていませんので、以下のコマンドを使用してパッケージがインストールされているかどうかを確認してください。

[root@centos Desktop]# rpm -qa|grep squid
squid-3.3.8-26.el7.x86_64

パッケージがインストールされていない場合は、yum install コマンドを使用してインストールします。

[root@centos Desktop]# yum install squid
Loaded plugins: fastestmirror, langpacks
Resolving Dependencies
--> Running transaction check
---> Package squid.x86_64 7:3.3.8-26.el7 will be installed
--> Processing Dependency: libecap.so.2()(64bit) for package:
 7:squid-3.3.8-26.el7.x86_64
--> Running transaction check
---> Package libecap.x86_64 0:0.2.0-9.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================
 Package          Arch            Version            Repository     Size
======================================================================
Installing:
 squid            x86_64          7:3.3.8-26.el7        base          2.6 M
Installing for dependencies:
 libecap          x86_64          0.2.0-9.el7           base           20 k

Transaction Summary
===========================================================================
Install  1 Package (+1 Dependent package)

Total download size: 2.6 M
Installed size: 8.6 M
Is this ok [y/d/N]: y
Downloading packages:
(1/2): libecap-0.2.0-9.el7.x86_64.rpm                      |  20 kB   00:17     
(2/2): squid-3.3.8-26.el7.x86_64.rpm                       | 2.6 MB   00:25     
----------------------------------------------------------------------------
Total                                              103 kB/s | 2.6 MB  00:25     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libecap-0.2.0-9.el7.x86_64                                1/2 
  Installing : 7:squid-3.3.8-26.el7.x86_64                               2/2 
  Verifying  : 7:squid-3.3.8-26.el7.x86_64                               1/2 
  Verifying  : libecap-0.2.0-9.el7.x86_64                                2/2 

Installed:
  squid.x86_64 7:3.3.8-26.el7                                                   

Dependency Installed:
  libecap.x86_64 0:0.2.0-9.el7                                                  

Complete!

サービスを開始し、起動時に開始されるように設定します。

[root@centos Desktop]# systemctl enable squid
Created symlink from /etc/systemd/system/multi-user.target.wants
/squid.service to /usr/lib/systemd/system/squid.service.
[root@centos Desktop]# systemctl start squid
[root@centos Desktop]# systemctl status squid
● squid.service - Squid caching proxy
   Loaded: loaded (/usr/lib/systemd/system/squid.service; enabled;
 vendor preset: disabled)
   Active: active (running) since Mon 2016-03-21 13:00:11 IST; 7s ago
  Process: 16554 ExecStart=/usr/sbin/squid $SQUID_OPTS -f $SQUID_CONF 
(code=exited, status=0/SUCCESS)
  Process: 16542 ExecStartPre=/usr/libexec/squid/cache_swap.sh 
(code=exited, status=0/SUCCESS)
 Main PID: 16601 (squid)
   CGroup: /system.slice/squid.service
           ├─16601 /usr/sbin/squid -f /etc/squid/squid.conf
           ├─16603 (squid-1) -f /etc/squid/squid.conf
           └─16657 (logfile-daemon) /var/log/squid/access.log

Mar 21 13:00:10 centos systemd[1]: Starting Squid caching proxy...
Mar 21 13:00:11 centos squid[16601]: Squid Parent: will start 1 kids
Mar 21 13:00:11 centos squid[16601]: Squid Parent:squid-1 process16603 ...ed
Mar 21 13:00:11 centos systemd[1]: Started Squid caching proxy.
Hint: Some lines were ellipsized, use -l to show in full.
[root@centos Desktop]#

Squid のメイン設定ファイルは /etc/squid/squid.conf にあります。

squid.conf ファイルのサンプルコードは次のとおりです。

#
Recommended minimum configuration:
#

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:
#
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

Only allow cachemgr access from localhost
http_access allow localhost manager
http_access deny manager

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 3128

Uncomment and adjust the following to add a disk cache directory.
#cache_dir ufs /var/spool/squid 100 16 256

Leave coredumps in the first cache dir
coredump_dir /var/spool/squid

#
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

コメントを外して以下を調整してキャッシュディレクトリを追加します。

Squid サービスを再起動し、Squid 3128 ポートを許可するファイアウォール ルールを追加します。

[root@centos Desktop]# systemctl restart squid

firewall-cmd –zone=public –add-port=3128/tcp –permanent

ルールを保存してサービスを再起動します。

firewall-cmd –reload

設定部分が完了したら、クライアントブラウザでsquid IPとデフォルトポートを指すブラウジングをテストします。

Firefox ブラウザを開き、[ツール] > [オプション] > [詳細] タブ > [ネットワーク] > [設定] > [手動プロキシ設定] ラジオ ボタンに移動し、squid サーバー IP 10.0.0.1 (ここではサーバー IP アドレスは 10.0.0.1 を使用しました) とポート 3128 を指定して、使用を確認します。すべてのプロトコルにこのプロキシ サーバーを使用し、[OK] をクリックします。

次のコマンドを使用して、squid access.log ファイルを参照してください。

tail -f /var/log/squid/access.log 

インターネットを楽しみながら、キャッシュ サーバーを使用して帯域幅を節約しましょう。

関連記事: