Ubuntu でプロキシサーバー(squid)のインストール

2021-07-02

Ubuntu 16.04 に squid 3.5 をインストール

ubuntu 16.04にプロキシサーバーとして機能する「squid 3.5」をインストールします。プロキシサーバーに接続する際ですが、BASIC認証を利用しユーザー名とパスワードを一致すれば使用できるようにします。また、プロキシサーバーの名前解決は、ステージング環境やテスト環境にあるサーバにも接続したいので「hosts」を優先的に利用します

squidのインストール

下記のUbuntuのバージョンに、squid をインストールします

# ubuntuのバージョン
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.4 LTS (Xenial Xerus)"

squidをインストールします

# squidをインストール
$ sudo apt install squid

# 以下のバージョンのsquidをインストールします
$ squid -v | head -3
Squid Cache: Version 3.5.12
Service Name: squid
Ubuntu linux

squidの接続に、BASIC認証を利用する

Squidで利用するBASIC認証は、htpasswdコマンドを利用してID・パスワードを発行します。
htpasswdコマンドはApacheに付属しているので、Apacheをインストールしていない場合はインストールする

$ sudo apt-get install apache2 apache2-utils

htpasswdコマンドを利用して、ID・パスワードを発行します。
認証用のパスワードファイル「/etc/squid/.htpasswd」として保存され、ユーザー名は「user1」とします

書式:htpasswd -c [パスワードファイル] [username]

$ sudo htpasswd -c /etc/squid/.htpasswd user1
New password:     # パスワード設定
Re-type new password:
Adding password for user user1

設定ファイル(/etc/squid/squid.conf )の編集

・BASIC認証関係に関する設定は、以下の項目をsquid.conf追加します

# basic認証に関する設定
auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/.htpasswd
auth_param basic children 5
auth_param basic realm Basic Authentication
auth_param basic credentialsttl 5 hours

# 認証用acl名を追加する
acl password proxy_auth REQUIRED

# パスワード認証を許可
http_access allow password

・名前解決はhostsファイルを優先的にするには、以下の項目をsquid.conf追加します

hosts_file /etc/hosts

 

私の利用するSquidの設定はファイルは以下となります

$ sudo vim /etc/squid/squid.conf 

# basic認証に関する設定
auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/.htpasswd
auth_param basic children 5
auth_param basic realm Basic Authentication
auth_param basic credentialsttl 5 hours

# ローカル環境に関する設定
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名を追加する
acl password proxy_auth REQUIRED

# SSL接続時 443ポート以外は拒否
acl SSL_ports port 443
acl CONNECT method CONNECT
http_access deny CONNECT !SSL_ports

# 接続先の設定、設定されていないポートは拒否など
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
http_access deny !Safe_ports
http_access allow localhost manager
http_access deny manager
http_access allow localnet
http_access allow localhost

# パスワード認証を許可
http_access allow password
http_access deny all

# 使用するポート
http_port 3128

# コアダンプを吐き出すディレクトリ
coredump_dir /var/spool/squid

# hostsファイルを参照する
hosts_file /etc/hosts

# キャッシュに関する設定
refresh_pattern ^ftp:		1440	20%	10080
refresh_pattern ^gopher:	1440	0%	1440
refresh_pattern -i (/cgi-bin/|\?) 0	0%	0
refresh_pattern (Release|Packages(.gz)*)$      0       20%     2880
refresh_pattern .		0	20%	4320

設定が終わったら、「squid」を起動します

$ sudo systemctl start squid

 

 

スポンサーリンク

0
0

UbuntuUbuntu

Posted by admin