天天看點

Squid代理伺服器&&搭建透明代理網關伺服器

案例需求

——公司選用RHEL5伺服器作為網關,為了有效節省網絡帶寬、提高區域網路通路Internet的速度,需要在網關伺服器上搭建代理服務,并結合防火牆政策實作透明代理,以減少用戶端的重複設定工作

<a target="_blank" href="http://img1.51cto.com/attachment/201007/110712345.jpg"></a>

需求描述

使用iptables設定SNAT政策

使192.168.2.0/24網段的主機通過NAT方式共享上網

配置squid代理服務

對HTTP通路進行緩存加速,并結合防火牆政策實作透明代理

在代理服務中進行通路控制

禁止區域網路使用者下載下傳rmvb、mp3格式的檔案

對超過3M大小的檔案不做緩存,禁止下載下傳超過8M的檔案

禁止使用者通路qq.com、tencent.com、xxxx.com等域的網站

啟用網址過濾,禁止通路包含“sex”、“adult”字樣的連結

實作思路

1. 準備好客戶機及Internet測試伺服器,正确配置各主機的網絡參數

<a target="_blank" href="http://img1.51cto.com/attachment/201007/7/1659430_1278471779CmbL.jpg"></a>

<a target="_blank" href="http://img1.51cto.com/attachment/201007/7/1659430_1278471780UuaZ.jpg"></a>

<a target="_blank" href="http://img1.51cto.com/attachment/201007/7/1659430_12784717829HP8.jpg"></a>

2. 區域網路主機将預設網關設為192.168.2.1

<a target="_blank" href="http://img1.51cto.com/attachment/201007/7/1659430_1278471783tRc9.jpg"></a>

3. 在測試伺服器上啟動httpd服務

<a target="_blank" href="http://img1.51cto.com/attachment/201007/7/1659430_1278471784tztc.jpg"></a>

4. 修改 squid.conf 檔案( /etc/squid/squid.conf )

<a target="_blank" href="http://img1.51cto.com/attachment/201007/7/1659430_12784717842B0T.png"></a>

<a target="_blank" href="http://img1.51cto.com/attachment/201007/7/1659430_1278471786Lul8.jpg"></a>

<a target="_blank" href="http://img1.51cto.com/attachment/201007/7/1659430_1278471789ZAaZ.jpg"></a>

<a target="_blank" href="http://img1.51cto.com/attachment/201007/7/1659430_1278471790yRqq.jpg"></a>

. . . 其他均保持預設即可(包括空行!)

5. 開啟路由轉發,添加實作透明代理的REDIRECT政策

<a target="_blank" href="http://img1.51cto.com/attachment/201007/7/1659430_127847179187jE.png"></a>

<a target="_blank" href="http://img1.51cto.com/attachment/201007/7/1659430_1278471791tK0p.png"></a>

6. 設定iptables的重定向政策

<a target="_blank" href="http://img1.51cto.com/attachment/201007/7/1659430_1278471792dS2v.jpg"></a>

Squid代理伺服器&amp;amp;&amp;amp;搭建透明代理網關伺服器

7. 初始化并啟動squid服務

<a target="_blank" href="http://img1.51cto.com/attachment/201007/7/1659430_1278471793MWc9.jpg"></a>

8. 在測試的httpd伺服器上,跟目錄下建立一個10M的檔案,驗證是否可以下載下傳

<a target="_blank" href="http://img1.51cto.com/attachment/201007/7/1659430_12784717942pjD.png"></a>

<a target="_blank" href="http://img1.51cto.com/attachment/201007/7/1659430_1278471796k5sN.jpg"></a>

驗證過程中,有一段時間需要我們淡定,認真等待一會兒就好了. . .

9. 客戶機無需做任何設定(除基本的IP及網關設定外)即可通路外網

Squid代理伺服器&amp;amp;&amp;amp;搭建透明代理網關伺服器

10. 檢視squid的測試伺服器的日志記錄,驗證代理是否成功

<a target="_blank" href="http://img1.51cto.com/attachment/201007/7/1659430_12784718026hTV.jpg"></a>

<a target="_blank" href="http://img1.51cto.com/attachment/201007/7/1659430_1278471803Nv67.jpg"></a>

. . . 完 。

附:

通路控制規則的比對順序

沒有設定任何規則時

—— 将拒絕所有用戶端的通路請求

有規則但找不到相比對的項時

—— 将采用與最後一條規則相反的權限,即如果最後一條規則是allow,那麼就拒絕用戶端的請求,否則允許該請求

squid軟體包基本配置

軟體包名:squid-2.6.STABLE6-3.el5

服務名:squid

主程式:/usr/sbin/squid

配置目錄:/etc/squid/

主配置檔案:/etc/squid/squid.conf

預設監聽端口:TCP 3128

預設通路日志檔案:/var/log/squid/access.log

繼續閱讀