0
1

More than 1 year has passed since last update.

3-1_OracleVM VirtualBox_Squid(途中

Last updated at Posted at 2023-02-15

1.Squidとは

Linux、Unixなどで普及しているプロキシキャッシュのこと。プロキシサーバ、ウェブキャッシュサーバなどで利用されているオープンソースソフトウェア。
Squidは、HTTP、HTTPS、FTPをはじめとする多くのプロトコルに対応している。

参考:https://www.designet.co.jp/faq/term/?id=c3F1aWQ

プロキシサーバとは内部ネットワークからインターネットへの接続を中継するサーバのこと。アクセス制御やウェブアクセスへの効率化のために利用される。

2.squidの設定ファイル

・squidの設定ファイル

/etc/squid/squid.conf

・ACL設定にてアクセスコントロールする条件を指定する

/etc/squid/squid.conf
【書式】
acl [ACL] [ACLタイプ] [条件]

(設定例その1)※送信元IPアドレスをACLで設定する場合
acl mynetwork src 192.168.56.0/24
(設定例その2)※宛先URLACLで設定する場合
acl requesturl dstdomain www.yahoo.co.jp

・http_access設定にてACLに対しての制御を行う

【書式】
http_access [allow|deny] [ACL名]

(設定例)
http_access allow mynetwork

・Squidが利用する待受ポートはデフォルトで「3128」
 プロキシサーバなどがHTTP接続を待ち受けるポートとしてよく用いられる「8080」に変更する

(変更前)     (変更後)
http_port 3128 → http_port 8080

・アクセスログ
 デフォルトでは読みづらいため、設定をする必要がある。
 以下の設定ではアクセスログの出力先を/var/log/squid/access.logとし
 ログフォーマットを指定している。

access_log /var/log/squid/access.log common
logformat squid %tl %6tr %>a %Ss/%03>Hs %<st %rm %ru %un %Sh/%<A %mt

・実際に表示されるログ出力例

192.168.56.1 - - [27/Sep/2021:22:54:21 +0900]
"CONNECT b97.yahoo.co.jp:443 HTTP/1.1" 200 20477 TCP_TUNNEL:HIER_DIRECT

アドレス:送信元
+0900:世界と日本の標準時間の差分
CONNECT:HTTP接続時のメソッド
b97.yahoo.co.jp:接続先URL
200:HTTPステータスコード
 ※HTTPステータスコードについて
 200:正常
 400台:クライアント側のエラー
 500台:サーバ側のエラー

3.簡単すぎる構成

     インターネット
   ↑
   ↑   HostPC
   ↑  ↓
   ↑  ↓ VirtualBox
  Squid ↲  ┛ ┗   OpenLDAP
   ↓            ↑
   ↓→→→→ユーザ認証→→→→↑

4.設定の流れ

事前準備

[root@testlinux ~]# setenforce 0
[root@testlinux ~]# systemctl stop firewalld
[root@testlinux ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since 日 2023-03-19 01:02:09 JST; 32min ago
     Docs: man:firewalld(1)
  Process: 739 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 739 (code=exited, status=0/SUCCESS)

squidのインストールを実施する。

[root@testlinux ~]# yum install squid
読み込んだプラグイン:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
~
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ squid.x86_64 7:3.5.20-17.el7_9.8 を インストール
~
---> パッケージ perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 を インストール
---> パッケージ perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

================================================================================
 Package                    アーキテクチャー
                                      バージョン               リポジトリー
                                                                           容量
================================================================================
インストール中:
 squid                      x86_64    7:3.5.20-17.el7_9.8      updates    3.1 M
依存性関連でのインストールをします:
 libecap                    x86_64    1.0.0-1.el7              base        21 k
 perl-Compress-Raw-Bzip2    x86_64    2.061-3.el7              base        32 k
 perl-Compress-Raw-Zlib     x86_64    1:2.061-4.el7            base        57 k
 perl-DBI                   x86_64    1.627-4.el7              base       802 k
 perl-Data-Dumper           x86_64    2.145-3.el7              base        47 k
 perl-Digest                noarch    1.17-245.el7             base        23 k
 perl-Digest-MD5            x86_64    2.52-3.el7               base        30 k
 perl-IO-Compress           noarch    2.061-2.el7              base       260 k
 perl-Net-Daemon            noarch    0.48-5.el7               base        51 k
 perl-PlRPC                 noarch    0.2020-14.el7            base        36 k
 squid-migration-script     x86_64    7:3.5.20-17.el7_9.8      updates     51 k

トランザクションの要約
================================================================================
インストール  1 パッケージ (+11 個の依存関係のパッケージ)

総ダウンロード容量: 4.5 M
インストール容量: 14 M
Is this ok [y/d/N]: y
Downloading packages:
~
--------------------------------------------------------------------------------
合計                                               1.5 MB/s | 4.5 MB  00:02     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
~
インストール:
  squid.x86_64 7:3.5.20-17.el7_9.8                                              

依存性関連をインストールしました:
  libecap.x86_64 0:1.0.0-1.el7                                                  
  perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7                                  
  perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7                                   
  perl-DBI.x86_64 0:1.627-4.el7                                                 
  perl-Data-Dumper.x86_64 0:2.145-3.el7                                         
  perl-Digest.noarch 0:1.17-245.el7                                             
  perl-Digest-MD5.x86_64 0:2.52-3.el7                                           
  perl-IO-Compress.noarch 0:2.061-2.el7                                         
  perl-Net-Daemon.noarch 0:0.48-5.el7                                           
  perl-PlRPC.noarch 0:0.2020-14.el7                                             
  squid-migration-script.x86_64 7:3.5.20-17.el7_9.8                             

完了しました!

[root@testlinux ~]# rpm -qa | grep squid
squid-migration-script-3.5.20-17.el7_9.8.x86_64
squid-3.5.20-17.el7_9.8.x86_64

インストール後squid.confを修正する。

[root@testlinux ~]# cp -p /etc/squid/squid.conf /etc/squid/squid.conf_bk
[root@testlinux ~]# ls -l /etc/squid/squid.conf*
-rw-r-----. 1 root squid 2315 10月 21 18:55 /etc/squid/squid.conf
-rw-r--r--. 1 root root  2315 10月 21 18:55 /etc/squid/squid.conf.default
-rw-r-----. 1 root squid 2315 10月 21 18:55 /etc/squid/squid.conf_bk

[root@testlinux ~]# vi /etc/squid/squid.conf

image.png

[root@testlinux ~]# diff /etc/squid/squid.conf /etc/squid/squid.conf_bk
8,12c8,12
< #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 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
18,24c18,24
< #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 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
26d25
< acl mynetwork src 192.168.1.22/24
53c52
< #http_access allow localnet
---
> http_access allow localnet
55d53
< http_access allow mynetwork
61c59
< http_port 8080
---
> http_port 3128
76,79d73
< 
< # Access log
< access_log /var/log/squid/access.log common
< logformat squid %tl %6tr %>a %Ss/%03>Hs %<st %rm %ru %un %Sh/%<A %mt
設定修正後の/etc/squid/squid.conf
[root@testlinux ~]# cat /etc/squid/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
acl mynetwork src 192.168.1.22/24

#
# 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
http_access allow mynetwork

# And finally deny all other access to this proxy
http_access deny all

# Squid normally listens to port 3128
http_port 8080

# 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

# Access log
access_log /var/log/squid/access.log common
logformat squid %tl %6tr %>a %Ss/%03>Hs %<st %rm %ru %un %Sh/%<A %mt
[root@testlinux ~]# systemctl restart squid.servicetus
[root@testlinux ~]# systemctl status squid.service
 squid.service - Squid caching proxy
   Loaded: loaded (/usr/lib/systemd/system/squid.service; disabled; vendor preset: disabled)
   Active: active (running) since  2023-03-19 01:43:39 JST; 10s ago
  Process: 5431 ExecStart=/usr/sbin/squid $SQUID_OPTS -f $SQUID_CONF (code=exited, status=0/SUCCESS)
  Process: 5420 ExecStartPre=/usr/libexec/squid/cache_swap.sh (code=exited, status=0/SUCCESS)
 Main PID: 5432 (squid)
    Tasks: 2
   CGroup: /system.slice/squid.service
           tq5432 /usr/sbin/squid -f /etc/squid/squid.conf
           mq5435 (squid-1) -f /etc/squid/squid.conf

 3 19 01:43:39 testlinux systemd[1]: Starting Squid caching proxy...
 3 19 01:43:39 testlinux squid[5431]: 2023/03/19 01:43:39| aclIpParseIpDa...'
 3 19 01:43:39 testlinux squid[5432]: Squid Parent: will start 1 kids
 3 19 01:43:39 testlinux systemd[1]: Started Squid caching proxy.
 3 19 01:43:39 testlinux squid[5432]: Squid Parent: (squid-1) process 543...d
Hint: Some lines were ellipsized, use -l to show in full.
[root@testlinux ~]# 

画像の通り、ネットワークの設定を行う。

アドレス:ローカルアドレス
ポート:上で設定した8080

image.png

ホストOSからなにかしらインターネットアクセスを行う。
/var/log/squid/access.log
を開いてログが出力されていることを確認する。

0
1
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
0
1