14
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【初心者】いまさらSquidに入門する

Posted at

1. はじめに

  • 職場にSquidサーバがあり、内部のサーバがインターネット上の資材ダウンロードなどを行う際のプロキシとして利用されている。
  • Squidのホワイトリストを編集したり、アクセスログを見ながら疎通確認したりする運用業務があるが、自分でSquidを最初から構築したことがなく、仕組みがよく分からないため、改めて基本を確認する。

2. やったこと

  • Amazon Linux 2023にSquidをインストールする。
  • アクセスログのフォーマット変更など、基本的な設定を行う。
  • クライアントからSquidサーバを指定して各種アクセスを行い、アクセスログの出方を確認する。

3. 構成図

image.png

4. 手順

4.1 インストール

Amazon Linux 2023にSquidをインストールし、サービスとして登録する。

[ec2-user@ip-10-0-0-27 ~]$ sudo dnf install squid
[ec2-user@ip-10-0-0-27 ~]$ squid -v
Squid Cache: Version 6.13

[ec2-user@ip-10-0-0-27 ~]$ sudo systemctl start squid
[ec2-user@ip-10-0-0-27 ~]$ sudo systemctl enable squid
Created symlink /etc/systemd/system/multi-user.target.wants/squid.service → /usr/lib/systemd/system/squid.service.

4.2 クライアントのSRCIP許可設定

クライアント(今回は10.0.1.0/24にあるEC2インスタンス)からのアクセスを許可するため、該当のCIDRからのアクセスを許可する設定を追加する。

  • もともと 10.0.0.0/8 が 「localnet」として登録されているため、そのまま 「localnet」からのアクセスを許可する設定行だけを追加する。設定変更後はサービス再起動が必要。
/etc/squid/squid.conf
acl localnet src 10.0.0.0/8  # RFC 1918 local private network (LAN)   ⇒もともと存在
http_access allow localnet ⇒この行を追加
  • 変更前後の動作(クライアントからのcurl)
クライアントのcurlコマンド
# 変更前(許可されていないためアクセス不可)
[ec2-user@ip-10-0-1-150 ~]$ curl --proxy http://10.0.0.27:3128  http://www.google.com/
~略~
<p>The following error was encountered while trying to retrieve the URL: <a href="http://www.google.com/">http://www.google.com/</a></p>
<blockquote id="error">
<p><b>Access Denied.</b></p>
</blockquote>
<p>Access control configuration prevents your request from being allowed at this time. Please contact your service provider if you feel this is incorrect.</p>
~略~

# 変更後(http://www.google.com へアクセス可)
[ec2-user@ip-10-0-1-150 ~]$ curl --proxy http://10.0.0.27:3128  http://www.google.com/
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="ja"><head><meta content="
~略~

4.3 待ち受けポート変更

待ち受けポートを変更する。

  • デフォルトは3128/tcpだが、自分としては8080/tcpがしっくりくるため、ポート番号を変更する。設定変更後はサービス再起動が必要。
/etc/squid/squid.conf
# Squid normally listens to port 3128
http_port 8080 ⇒ この行を 3128 から 8080 に変更
  • 設定変更後の動作(クライアントからのcurl)
クライアントのcurlコマンド
# 3128/tcp ではアクセス不可
[ec2-user@ip-10-0-1-150 ~]$ curl --proxy http://10.0.0.27:3128  http://www.google.com/
curl: (7) Failed to connect to 10.0.0.27 port 3128 after 0 ms: Could not connect to server

# 8080/tcp でアクセス可
[ec2-user@ip-10-0-1-150 ~]$ curl --proxy http://10.0.0.27:8080 https://www.google.com/ 
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="ja"><head><meta content="
~略~

4.4 アクセスログフォーマット変更

デフォルトのアクセスログフォーマットだと、時刻表示がUNIXタイムになっており見づらいため、普通の日時表示に変更する。

  • アクセスログのフォーマットパターンがSquid内にいくつか用意されており、デフォルトは「squid」になっているが、今回は内容が見やすいと感じた「combined」に変更する。設定変更後はサービス再起動が必要。
/etc/squid/squid.conf
access_log daemon:/var/log/squid/access.log combined   ⇒この行を追加
  • 変更前後の動作(クライアントからのcurl)
クライアントのcurlコマンド
[ec2-user@ip-10-0-1-150 ~]$ curl --proxy http://10.0.0.27:8080  http://www.google.com/
/var/log/squid.access.log
# 設定変更前(フォーマット:squid)
1754892839.574     97 10.0.1.150 TCP_MISS/200 20002 GET http://www.google.com/ - HIER_DIRECT/142.250.206.196 text/html

# 設定変更後(フォーマット:combined)
10.0.1.150 - - [11/Aug/2025:07:11:30 +0000] "GET http://www.google.com/ HTTP/1.1" 200 19967 "-" "curl/8.11.1" TCP_MISS:HIER_DIRECT

4.5 ホワイトリスト設定

プロキシ経由でアクセス可能な外部サイトをホワイトリスト形式で定義する。

  • アクセスを許可する外部サイトドメインを whitelist ファイルに追加する。
/etc/squid/whitelist
.google.com
.yahooabcde.co.jp
  • whitelist を使用するための設定を squid.conf に追加する。
    • 4.2項で、「localnet」からのアクセスを許可する設定行「http_access allow localnet」を設定したが、この行があると、localnetからは全ての外部サイトへのアクセスが許可されてしまうため、この行は削除する。
    • 改めて「http_access allow localnet whitelist」を追加する。この行は、「localnetからのアクセス "かつ" whitelistに記載されたドメインへのアクセス」を許可するという意味。
    • 設定変更後はサービス再起動が必要。
/etc/squid/squid.conf
acl whitelist dstdomain "/etc/squid/whitelist"  ⇒ 追加

http_access allow localnet ⇒ この行は削除
http_access allow localnet whitelist ⇒ 追加
  • 変更後の動作(クライアントからのcurl)
クライアントのcurlコマンド
# www.google.com (whitelist登録あり)へはアクセス可
[ec2-user@ip-10-0-1-150 ~]$ curl --proxy http://10.0.0.27:8080 https://www.google.com
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="ja"><head><meta content="
~略~

# www.yahoo.co.jp (whitelist登録なし)へはアクセス不可
[ec2-user@ip-10-0-1-150 ~]$ curl --proxy http://10.0.0.27:8080  http://www.yahoo.co.jp
~略~
<p>The following error was encountered while trying to retrieve the URL: <a href="http://www.yahoo.co.jp/">http://www.yahoo.co.jp/</a></p>

<blockquote id="error">
<p><b>Access Denied.</b></p>
</blockquote>

<p>Access control configuration prevents your request from being allowed at this time. Please contact your service provider if you feel this is incorrect.</p>
~略~

4.6 アクセスログの確認

業務でSquid経由の疎通確認時をすることがあり、疎通エラーになった時に切り分けができるように、アクセスログのパターンをいくつか確認する(クライアント(curl)側のメッセージと、Squidのアクセスログをセットで記載)。

httpアクセス成功

クライアントのcurlコマンド
[ec2-user@ip-10-0-1-150 ~]$ curl --proxy http://10.0.0.27:8080  http://www.google.com
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="ja"><head><meta content="
~略~
/var/log/squid/access.log
10.0.1.150 - - [13/Aug/2025:08:34:23 +0000] "GET http://www.google.com/ HTTP/1.1" 200 19926 "-" "curl/8.11.1" TCP_MISS:HIER_DIRECT
  • httpステータスコードが200
  • TCP_MISS:HIER_DIRECT : キャッシュミスで直接オリジンからコンテンツ取得

httpsアクセス成功

クライアントのcurlコマンド
[ec2-user@ip-10-0-1-150 ~]$ curl --proxy http://10.0.0.27:8080  https://www.google.com
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="ja"><head><meta content="
~略~
/var/log/squid/access.log
10.0.1.150 - - [13/Aug/2025:08:34:26 +0000] "CONNECT www.google.com:443 HTTP/1.1" 200 23442 "-" "curl/8.11.1" TCP_TUNNEL:HIER_DIRECT
  • httpステータスコードが200
  • TCP_TUNNEL:HIER_DIRECT : キャッシュミスで直接オリジンからコンテンツ取得

ドメインのwhitelist登録がなく、httpアクセスが失敗

クライアントのcurlコマンド
[ec2-user@ip-10-0-1-150 ~]$ curl --proxy http://10.0.0.27:8080  https://www.yahoo.co.jp
~略~
<p>The following error was encountered while trying to retrieve the URL: <a href="http://www.yahoo.co.jp/">http://www.yahoo.co.jp/</a></p>

<blockquote id="error">
<p><b>Access Denied.</b></p>
</blockquote>

<p>Access control configuration prevents your request from being allowed at this time. Please contact your service provider if you feel this is incorrect.</p>
~略~
/var/log/squid/access.log
10.0.1.150 - - [13/Aug/2025:08:34:37 +0000] "GET http://www.yahoo.co.jp/ HTTP/1.1" 403 3960 "-" "curl/8.11.1" TCP_DENIED:HIER_NONE
  • httpステータスコードが403
  • TCP_DENIED:HIER_NONE : squidがアクセスを拒否

ドメインのwhitelist登録はあるが、Squidが宛先ドメインの名前解決できず、結果として接続できずに失敗

クライアントのcurlコマンド
[ec2-user@ip-10-0-1-150 ~]$ curl --proxy http://10.0.0.27:8080  http://www.yahooabcde.co.jp
~略~
<p>The following error was encountered while trying to retrieve the URL: <a href="http://www.yahooabcde.co.jp/">http://www.yahooabcde.co.jp/</a></p>

<blockquote id="error">
<p><b>Unable to determine IP address from host name <q>www.yahooabcde.co.jp</q></b></p>
</blockquote>

<p>The DNS server returned:</p>
<blockquote id="data">
<pre>Name Error: The domain name does not exist.</pre>
</blockquote>

<p>This means that the cache was not able to resolve the hostname presented in the URL. Check if the address is correct.</p>
~略~
/var/log/squid/access.log
10.0.1.150 - - [13/Aug/2025:08:34:43 +0000] "GET http://www.yahooabcde.co.jp/ HTTP/1.1" 503 4210 "-" "curl/8.11.1" TCP_MISS_ABORTED:HIER_NONE
  • httpステータスコードが503
  • TCP_MISS_ABORTED:HIER_NONE : squidがオリジンへのアクセスを中断

ドメインのwhitelist登録はあるが、squidが宛先ドメインのサーバにアクセスできずに失敗

  • このケースの実施時、squidサーバからインターネットへのOutbound通信を遮断
クライアントのcurlコマンド
[ec2-user@ip-10-0-1-150 ~]$ curl --proxy http://10.0.0.27:8080  http://www.google.com
~略~
<p>The following error was encountered while trying to retrieve the URL: <a href="http://www.google.com/">http://www.google.com/</a></p>
<blockquote id="error">
<p><b>Connection to 142.250.76.132 failed.</b></p>
</blockquote>
<p id="sysmsg">The system returned: <i>(110) Connection timed out</i></p>
<p>The remote host or network may be down. Please try the request again.</p>
~略~
/var/log/squid/access.log
10.0.1.150 - - [13/Aug/2025:08:46:25 +0000] "GET http://www.google.com/ HTTP/1.1" 503 4053 "-" "curl/8.11.1" TCP_MISS_TIMEDOUT:HIER_DIRECT
  • httpステータスコードが503
  • TCP_MISS_TIMEDOUT:HIER_DIRECT : squidがオリジンへのアクセスをタイムアウトで中止

5. 所感

  • とりあえず業務で必要な内容の範囲についてはなんとなく理解することができた。
  • クライアント側のエラーメッセージとsquid側のアクセスログをセットで見ればだいたいトラシューができそう。
  • httpsの途中復号(SSL Bump)、認証、キャッシュなど、squidも奥が深く、機会があればまた別の機能について深堀したい。

6. 参考リンク

  • インストール

  • アクセスログ

  • ホワイトリスト

14
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?