LoginSignup
4
1

More than 3 years have passed since last update.

[Alibaba Cloud] 多段プロキシ構成をECS(VM)で構築してみる

Last updated at Posted at 2019-06-17

まずはじめに

もし気に入っていただけたり、役に立ったらぜひ ”いいね!” お待ちしております!^^¥

この記事の目的

Alibaba Cloudを使ってさくっと多段プロキシ構成を検証してみる。
ゴールイメージ↓
image.png

プロキシサーバー(Proxy Server)とは

プロキシサーバとは、クライアントからサーバへhttp通信する際、直接やり取りするのが好ましくない、
および(もしくは) 効率的で無い場合に利用される代理応答サーバのことです。
プロキシサーバの具体的なメリット・必要性は以下の通りです。

  • 悪意あるユーザからの直接アクセスを防ぐことによりセキュリティを向上できる
  • 社内のインターネットアクセスをプロキシサーバへ集中させることにより、誰がいつどのようなサイトを見ているか、といったログ情報を集中管理できる。
  • ユーザが閲覧したサイトのコンテンツをプロキシサーバのHDD等のディスクにキャッシュし、2人目以降が同じサイトを見ようとする際にキャッシュから応答することでインターネット通信量を減らし、効率化できる

Alibaba Cloudを選定した理由

  • とりあえず初期登録で年間3万円の無料クーポンが付いている
    • AWSだと無料枠のインスタンスが決まっており、下手にいじくり回すと思わぬ課金が走るので、Alibaba Cloudを選定しました。
  • 安い
    • 従量課金/サブスクリプションの2種類選択でき、従量の単価も比較的安い。日本リージョンでLinuxのサーバーの最小スペックを選べば1000円程度で毎月運用可能。
  • アジアリージョン(特に中国)の選択肢が豊富
    • 中国国内だけで8つリージョンがある
    • 東南アジアリージョンが豊富
    • リージョン一覧↓

image.png

兎にも角にもサーバーを買って、セットアップしてみる

1vCPU 1GiB(メモリ)でdisk20がついてこんなもん。(1.64円/1時間)
検証用で使わない手はないですね。
多段プロキシ構成なので、2つインスタンスを作ります。
image.png

ホスト名はそれぞれ、

  • proxy-forwarder(下位プロキシ)
  • proxy-above(上位プロキシ)

としました。

image.png

多段プロキシ専用のセキュリティグループを作成します。
Squidのアプリケーションポートとして、8080をセットしますので、8080ポート[受信]を公開しましょう。
image.png

ECSの設定

ECS(仮想インスタンス)の購入が完了しましたらSSHでログインしましょう。
今回はCentOSで構築していきます。
便宜上rootアカウントで操作していきます。

# ssh root@'ECSグローバルIP' -i 'YOUR PEM KEY ' or -p 'パスワード'

Squidインストール ~ 設定

初期でSquidは入っていないので、yum installしてあげます。
インストール完了後whichコマンドでインストールを確認します。

# yum install squid -y
# which squid
/usr/sbin/squid

squid.confの設定

vi(vimでもok)エディタで編集していきましょう。

# vim /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

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

上位プロキシの設定

アクセスを許可するローカルネットワークを設定します。
社内アクセスのみを許可したいのであれば、社内の固定IPアドレスを記載しましょう。


acl localnet src 'アクセスを許可したいIPアドレス'

ポートを8080へ変更します。

# Squid normally listens to port 3128
http_port 8080

Squidをスタートさせましょう。
enableでサーバー立ち上げ時に自動起動できるようにします。

# systemctl start squid
# systemctl enable squid

下位プロキシの設定

forwardする下位プロキシの設定を行います。
上位プロキシにフォワードするための設定や、フォワードリストの作成など、少し細かい作業が必要になります。SSHログインなどはさきほどと同じなので、割愛します。

Squidインストール

# yum install squid -y
# which squid
/usr/sbin/squid

アクセスを許可するローカルネットワークを設定します。
社内アクセスのみを許可したいのであれば、社内の固定IPアドレスを記載しましょう。

acl localnet src 'アクセスを許可したいIPアドレス'

ポートを8080へ変更します。

# Squid normally listens to port 3128
http_port 8080

フォワードリストを作成します。
今回は、iPアドレス確認君と、ヤフーのトップサイトを2つ設定します。
www. 以降のURIを設定するクセがSquidにはあるので、注意ください。


# vi /etc/squid/
.ugtop.com
.yahoo.co.jp

さきほど作成したフォワードリストをsquid.conf内に追記します。

acl whitelist dstdomain "/etc/squid/whitelist"

上位プロキシへフォワードする設定を入れます。
今回、上位プロキシのプライベートIPは下図の赤枠なので、以下のように書きます。
ICP番号はデフォルトでは3130となります。
icp ポートを用いて上位プロキシのキャッシュを利用しない場合は、icp ポートに7を指定します。

image.png

cache_peer 10.0.0.244 parent 8080 3130 no-query
cache_peer_access 10.0.0.244 allow whitelist
never_direct allow whitelist

Squidをスタートさせましょう。
enableでサーバー立ち上げ時に自動起動できるようにします。


# systemctl start squid
# systemctl enable squid

プロキシ設定と接続確認

自分はMacOS Xなので、以下の手順で”上位”プロキシサーバーを設定します。

  • システム設定
  • ネットワーク
  • 詳細
  • プロキシ
  • HTTP/HTTPSにプロキシのグローバルIPとポート番号を設定 image.png

whitelistに入っていないIPアドレス確認サービスでアクセス元IPを確認します。
image.png

次に、whitelistに入れたIPアドレス確認サイトにアクセスします。
ちゃんと、上位プロキシのグローバルIPでアクセスできていることが確認できました!

image.png

yahooのページも見れています。
接続元IPは確認できないですが、きっと上位プロキシからアクセスされているでしょう...
image.png

全体config

上位プロキシ


#
# 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 localnet src '任意のIP'

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


下位プロキシ


#
# 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 localnet src '任意のIP'

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


acl whitelist dstdomain "/etc/squid/whitelist"
cache_peer 10.0.0.244 parent 8080 3130 no-query
cache_peer_access 10.0.0.244 allow whitelist
never_direct allow whitelist

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