プロキシサーバ(Squid) - CentOS|Systems Engineer Wiki

Systems Engineer Wiki

訪問者:23,217,409 人目

<< 2020年12月 

12345
6789101112
13141516171819
20212223242526
2728293031
お知らせ・メンテナンス情報
  >  
  >  
プロキシサーバ(Squid)

プロキシサーバ(Squid)

投稿日:2015-01-02 00:03:18

プロキシサーバ(Squid)

CentOS 6.X プロキシ(Proxy)環境の構築手順を解説します。 プロキシサーバを構築するには、squidというモジュールをサーバにインストールします。 モジュールのインストールは、管理者(root)権限で作業を行う必要があります。 また、今回のプロキシサーバは外部サーバを利用した場合という点を想定し設定している為、社内(ローカルネットワーク)ようでのプロキシを利用する想定ではありません。

# yum install squid

続いてインストールした、モジュールの設定ファイルを変更します。

# vi /etc/squid/squid.conf

設定ファイルを開きます。 Viコマンドなどを利用して設定ファイルを編集して行きます。

# # Recommended minimum configuration: # acl manager proto cache_object acl localhost src 127.0.0.1/32 ::1 acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1 # Example rule allowing access from your local networks. # Adapt to list your (internal) IP networks from where browsing # should be allowed #acl all src 0.0.0.0/0.0.0.0 #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 example src XXX.XXX.XXX.XXXX/32 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: # # Only allow cachemgr access from localhost http_access allow manager localhost http_access deny manager # 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 # 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 example # And finally deny all other access to this proxy http_access deny all # Squid normally listens to port 3128 #http_port 3128 http_port 8080 # We recommend you to use at least the following line. hierarchy_stoplist cgi-bin ? # Uncomment and adjust the following to add a disk cache directory. cache_mem 256 MB 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 request_header_access Referer deny all request_header_access X-Forwarded-For deny all request_header_access Via deny all request_header_access Cache-Control deny all visible_hostname unknown forwarded_for off

赤色の部分がコメントアウトした箇所。 緑色の部分が新しく追記した箇所になります。

acl example src XXX.XXX.XXX.XXXX/32

この箇所に接続元のグローバルIPアドレス(ISP)のIPアドレスを設定します。 今回は接続もと固定IPを一意に設定する為、/32という設定をしています。

誤:#acl CONNECT method CONNECT 正:acl CONNECT method CONNECT

私も記述ミスを良くするのですが、ACLの記述を閉じ忘れてプロキシサーバ起動時にエラーが発生する事が良くあります。

http_access allow example

ACLの設定内容をロードします。

http_port 8080

プロキシサーバの接続ポートを8080に設定します。 こちらは任意で好きなポート番号が設定可能です。

visible_hostname unknown

プロキシサーバ名称(ホスト名)を秘匿化します。

forwarded_for off

プロキシサーバのIPアドレスを秘匿化します。

/etc/init.d/squid start

サービスを起動します。

chkconfig squid on

サービスの自動起動を設定します。

ファイアウォール設定

# vi /etc/sysconfig/iptables

プロキシサーバで利用するポート番号をファイアウォールに追記しポート番号の解放設定を行います。 Viコマンドなどを利用して設定ファイルを編集して行きます。

# Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT

今回は8080番号を使用するため「-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT」と記述を追記します。 サーバによっては、他のファイアウォール設定が行われている事もある為十分に注意して作業を行うようにして下さい。 また、設定内容は必ず「*filter」よりも後ろかつ「COMMIT」よりも前に記述するようにして下さい。

# /etc/init.d/iptables restart

サービスを再起動して設定を有効化します。

サーバ側の設定が完了したらクライアントPCにプロキシサーバの設定を追記すればプロキシサーバ経由での接続が可能になります。 設定内容は、「サーバIP(XXX.XXX.XXX.XXX):8080」と左記のような設定を行います。 今回はプロキシサーバのポート番号を8080に設定しているため、8080をポート番号に設定しております。 接続の確認方法は、IP確認君などで自分のグローバルIPがプロキシサーバのIPに変更されていれば、プロキシサーバ経由でインターネット接続が行えているという事になります。