コンテンツにスキップ

EC2インスタンスにSquidを構築する#

ここでは AmazonLinux2 で構築したEC2インスタンスにSquidをインストールしていきます。実行コマンドはOSのパッケージ管理ツールによって変わる可能性があります。

1. Squidのインストール#

1.1 最初に、起動済みのEC2インスタンスへログインして下記コマンドを実行します。このコマンドでインストールされるSquidのバージョンを確認します。

yum list squid
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Available Packages
squid.x86_64                                   7:3.5.20-17.amzn2.6.1                                    amzn2-core

1.2 バージョン確認後、下記コマンドでSquidをインストールします。

sudo yum install squid

1.3 インストール後、バージョンとlistenポート(デフォルト3128)を確認します。

squid -v
Squid Cache: Version 3.5.20

sudo lsof -i:3128
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
squid   6229 squid   11u  IPv6  58800      0t0  TCP *:squid (LISTEN)

1.4 (Option)次に自動起動設定を追加します。

sudo systemctl enable squid
sudo systemctl restart squid

2. Squidの初期設定#

2.1 必要に応じて設定変更を行います。

sudo vi /etc/squid/squid.conf
下記に一例を上げます。aclは要件に応じて設定をお願いします。待受ポートも変更が可能なため、セキュリティ要件に合わせて変更をお願い致します。

要素 説明
acl プロキシへのアクセスを制御リスト
http_port クライアントリクエストをlistenするポート。デフォルト3128

2.2 ACLの例を紹介します。下記はデフォルトで入っている設定です。RFCで定義されているInternal networkからのアクセス制御を定義しています。そして、それを後半でallowしていることがわかります。

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

〜〜〜

# 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 ★定義したaclをallow
http_access allow localhost

2.3 次はポートのACL例を紹介します。下記もデフォルトで入っている設定です。よく利用されるポートやレンジについてアクセス制御が定義されています。そして、! を利用することで指定のポート以外をdenyしています。

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

〜〜〜

#
# Recommended minimum Access Permission configuration:
#
# Deny requests to certain unsafe ports
http_access deny !Safe_ports ★指定ポート以外はdeny

2.4 下記は、listenするポートを指定します。

# Squid normally listens to port 3128
http_port 3128

2.5 上記を変更の上、下記コマンドで反映させてください。

systemctl reload squid

2.2 上記が完了したら、もう一台のEC2インスタンスにも同様の対応を実施します。