Proxy Server - 安裝與基本設定

Proxy 基本上來說就是一個代理伺服器,當然如果在對外網路頻寬不足的話使用 Proxy 可以有效的舒解網路流量,這篇文章中,小弟會介紹如何建立一個簡單的 Proxy Server,其中包含了如何讓單位的使用者透過你的 Proxy 連上網頁,還有一些簡單的設定。

對於這個 Proxy 的單元,小弟會不定期的加入一些應用,當然對於正在服兵役的我,文章的出產時間會有一段時間,若您覺得文章對您或其它網友有幫助,歡迎轉貼出去,當然也煩請加註出處。

安裝 Squid-Caching Proxy Server

對於 Linux 來說,使用 Squid 這個八角章魚的套件是最熱門的 Caching Server 了,我在這單元裡將會介紹如何編譯並安裝。

下載 squid

你可以到 Squid-Cache 網站上下載最新版本的 squid 套件,在台灣地區則可以選擇 Squid-Cache 的 mirror 站台。在本文完成時,最新版本的 squid 穩定版本是 2.6。

0001
0002
0003
0004
root # wget http://www2.tw.squid-cache.org/Versions/v2/2.6/squid-2.6.STABLE3.tar.bz2
root # tar -jxvf squid-2.6.STABLE3.tar.bz2
root # ./configure --prefix=/usr/local/squid --enable-gnuregex --enable-async-io=80 --enable-kill-parent-hack --enable-snmp --disable-ident-lookups --enable-cahce-digests --enable-poll --enable-linux-netfilter
root # make

安裝 squid

經過以上的動作之後會把 squid 編譯完成,請執行 make install 完成安裝。

0001
root # make install

設定 Squid

最簡單的設定

squid 的設定檔是在 /usr/local/squid/etc/squid.conf 這個檔案

0001
0002
0003
0004
0005
0006
0007
0008
0009
root # vi squid.conf
---------------------------------------------------------------------
# 設定 http 的 port
http_port 3128
# 設定暫存檔的路徑
cache_dir ufs /usr/local/squid/var/cache 100 16 256
# 設定 squid 是由 nobody 的身份啟動
cache_effective_user nobody
---------------------------------------------------------------------

建立暫存目錄

0001
0002
root # mkdir -p /usr/local/squid/var/cache
root # chown -Rf nobody /usr/local/squid

第一次啟動 squid ...

當你第一次啟動 squid 時,因為在暫存目錄的 cache spool 都還沒有建立起來,所以必需要把這些目錄都建立起來之後才可以正常使用 squid。

0001
root # /usr/local/squid/sbin/squid -z

當你執行完上面的指令之後,可以看到 /usr/local/squid/var/cache 多了很多目錄

0001
0002
0003
root # ls /usr/local/squid/var/cache
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
root #

現在你可以真的開始使用 squid 了。

若要執行 squid,可以使用 RunCache 這個 shell script 可以很簡單的啟動:

0001
0002
0003
root # netstat -ntulp | grep 3128
tcp 0 0 0.0.0.0:3128 0.0.0.0:* LISTEN 2998/squid
root #

查看 squid 是否正在執行

0001
root # /usr/local/bin/RunCache &

設定 Browser 透過 Proxy 開啟網頁

以下為 Firefox 的設定,若是 IE 的畫請比照參考。

現在你應該可以使用 proxy 上網了!

設定使用者也可以透過你的 Proxy 上網

當你在你的 Client 設定好 proxy 之後,有可能會發現會出現類似以下畫面。

Squid - Access Denied.

這表示你的 squid 不允許你的 Client 透過 Proxy 存取網頁,所以現在我們就來加入一個條件可以幫內部的 Client 做代理。

設定 squid.conf

0001
0002
0003
0004
0005
0006
0007
root # vi /usr/local/etc/squid.conf
---------------------------------------------------------------------
# 建立一個 acl 為 l-penguin
acl l-penguin src 192.168.1.0/24
# 設定 l-penguin 可以使用 http 存取
http_access allow l-penguin
---------------------------------------------------------------------

現在在來看看是否可以瀏覽網頁!

0001
0002
root # kill 2998
root # /usr/local/bin/RunCache &

Squid - Connect by proxy

09/08/2006


首頁