CENTOS 6.6 下squid 安裝配置

來源:互聯網
上載者:User

標籤:伺服器安全   Proxy 伺服器   blank   模型   target   

一、Proxy 伺服器

Proxy 伺服器(Proxy Server)是一種重要的伺服器安全功能,它的工作主要在開放系統互聯(OSI)模型的會話層,從而起到防火牆的作用。Proxy 伺服器大多被用來串連INTERNET(國際互連網)和Local Area Network(區域網路)。

Squid cache(簡稱為Squid)是一個流行的自由軟體,它符合GNU通用公用許可證。Squid作為網頁伺服器的前置cache伺服器,可以代理使用者向web伺服器請求資料並進行緩衝,也可以用在區域網路中,使區域網路使用者通過代理上網。Squid主要設計用於在Linux一類系統運行。

squid 分類

按照代理類型的不同,可以將Squid 代理分為正向 Proxy和反向 Proxy,正向 Proxy中,根據實現方式的不同,又可以分為普通代理和透明代理。

· 普通代理:需要客戶機在瀏覽器中指定Proxy 伺服器的地址、連接埠;

· 透明代理:適用於企業的網關主機(共用接入Internet)中,客戶機不需要指定Proxy 伺服器地址、連接埠等資訊,Proxy 伺服器需要設定防火牆策略將客戶機的Web訪問資料轉交給代理服務程式處理;

· 反向 Proxy:是指以Proxy 伺服器來接受internet上的串連請求,然後將請求轉寄給內部網路上的伺服器,並將從伺服器上得到的結果返回給internet上請求串連的用戶端,此時Proxy 伺服器對外就表現為一個伺服器。

官方地址:http://www.squid-cache.org/

參考文檔:http://www.squid-cache.org/Doc/config/

作業系統:CentOS release 6.4 (Final)

二、系統內容

作業系統:CentOS release 6.6 (Final)

Squid版本:squid-3.1.10-29.el6.i686

SELINUX=disabled

HTTP Service: stoped

防火牆:普通代理模式關閉、其他模式開啟

三、安裝Squid服務

3.1 檢查squid軟體是否安裝

# rpm -qa|grep squid

3.2 如果未安裝,則使用yum 方式安裝

# yum -y install squid

650) this.width=650;" title="image" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M01/6C/88/wKiom1VLI4DA3vczAAGTKFd1zXs432.jpg" width="558" height="367" />

3.3 設定開機自啟動

chkconfig --level 35 squid on             //在3、5層級上自動運行squid服務

四、squid伺服器的設定檔說明

http_port 3128 //設定監聽的IP與連接埠號碼

cache_mem 64 MB //額外提供給squid使用的記憶體,squid的記憶體總佔用為 X * 10+15+“cache_mem”,其中X為squid的cache佔用的容量(以GB為單位),比如下面的cache大小是100M,即0.1GB,則記憶體總佔用為0.1*10+15+64=80M,推薦大小為實體記憶體的1/3-1/2或更多。

maximum_object_size 4 MB //設定squid磁碟緩衝最大檔案,超過4M的檔案不儲存到硬碟

minimum_object_size 0 KB //設定squid磁碟緩衝最小檔案

maximum_object_size_in_memory 4096 KB //設定squid記憶體緩衝最大檔案,超過4M的檔案不儲存到記憶體

cache_dir ufs /var/spool/squid 100 16 256 //定義squid的cache存放路徑 、cache目錄容量(單位M)、一級緩衝目錄數量、二級緩衝目錄數量

logformat combined %&gt;a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}&gt;h" %Ss:%Sh //log檔案日誌格式

access_log /var/log/squid/access.log combined //log檔案存放路徑和日誌格式

cache_log /var/log/squid/cache.log //設定緩衝日誌

logfile_rotate 60 //log輪循 60天

cache_swap_high 95 //cache目錄使用量大於95%時,開始清理舊的cache

cache_swap_low 90 //cache目錄清理到90%時停止。

acl localnet src 192.168.1.0/24 //定義本地網段

http_access allow localnet //允許本地網段使用

http_access deny all //拒絕所有

visible_hostname centossd //主機名稱

cache_mgr [email protected] //管理員郵箱

五、普通代理服務

即標準的、傳統的代理服務,需要客戶機在瀏覽器中指定Proxy 伺服器的地址、連接埠。

實驗拓撲圖如下:

650) this.width=650;" title="clip_image004" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image004" src="http://s3.51cto.com/wyfs02/M02/6C/88/wKiom1VLI4GjpksPAABh7FwKpb8219.jpg" width="558" height="228" />

5.1 配置Squid Proxy 伺服器IP地址

將eth0的IP地址修改為192.168.1.168(內網地址)

將eth1的IP地址修改為192.168.0.128(外網地址)

配置好後如所示:

650) this.width=650;" title="clip_image006" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image006" src="http://s3.51cto.com/wyfs02/M00/6C/84/wKioL1VLJPWANZjrAAIFPzbW0pM813.jpg" width="558" height="393" />

5.2 編輯squid 主設定檔/etc/squid/squid.conf

把已存在的設定檔備份mv /etc/squid/squid.conf /etc/squid/squid.conf.bak

編輯設定檔vi /etc/squid/squid.conf

http_port 3128

cache_mem 64 MB

maximum_object_size 4 MB

cache_dir ufs /var/spool/squid 100 16 256

access_log /var/log/squid/access.log

acl localnet src 192.168.1.0/24

http_access allow localnet

http_access deny all

visible_hostname centossd

cache_mgr [email protected] // 可以不添加

5.3 初始化

# squid –z

幾個常用命令

650) this.width=650;" title="clip_image007" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image007" src="http://s3.51cto.com/wyfs02/M01/6C/84/wKioL1VLJPWRG3tkAACW9F4SFxI938.jpg" width="535" height="62" />

5.4 啟動Squid

650) this.width=650;" title="clip_image008" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image008" src="http://s3.51cto.com/wyfs02/M02/6C/84/wKioL1VLJPWTiI_HAAA4piLWrdo227.jpg" width="566" height="41" />

5.5 配置Web 服務器

設定管理員再次不做詳述

查看web伺服器ip地址

650) this.width=650;" title="clip_image010" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image010" src="http://s3.51cto.com/wyfs02/M00/6C/84/wKioL1VLJPah_aeZAADqs2N6KyQ795.jpg" width="557" height="157" />

5.6 配置用戶端IP地址

650) this.width=650;" title="clip_image012" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image012" src="http://s3.51cto.com/wyfs02/M01/6C/84/wKioL1VLJPbjFsuhAADAZ6-aEL0350.jpg" width="311" height="303" />

5.7 配置瀏覽器代理

650) this.width=650;" title="clip_image014" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image014" src="http://s3.51cto.com/wyfs02/M02/6C/84/wKioL1VLJPfzKR_vAAGSRTuSq0I739.jpg" width="557" height="341" />

5.8 測試

650) this.width=650;" title="clip_image016" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image016" src="http://s3.51cto.com/wyfs02/M00/6C/84/wKioL1VLJPjyPnpNAAI-emdbdzc243.jpg" width="557" height="502" />

測試成功。

如若想測試網域名稱訪問可以通過設定本地dns或者修改hosts檔案來測試,此處不做詳述。

通過可以看到用戶端可以和Proxy 伺服器端通訊,不能和web伺服器通訊。

5.9 測試錯誤頁面

650) this.width=650;" title="clip_image018" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image018" src="http://s3.51cto.com/wyfs02/M01/6C/84/wKioL1VLJPjRrtINAAEEp0pTA9I242.jpg" width="631" height="355" />

可以看到在squid 設定檔裡設定的參數在錯誤頁面裡的顯示。

六、透明代理服務

適用於企業的網關主機,客戶機不需要指定Proxy 伺服器地址、連接埠等資訊,通過iptables將客戶機的Web訪問資料轉交給代理服務程式處理。

實驗拓撲圖如下:

650) this.width=650;" title="clip_image020" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image020" src="http://s3.51cto.com/wyfs02/M02/6C/84/wKioL1VLJPmQIwySAABonhi4bnU286.jpg" width="557" height="234" />

6.1 修改squid 主設定檔/etc/squid/squid.conf

在http_port 3128 後添加transparent 關鍵字。

http_port 3128 transparent

cache_mem 64 MB

maximum_object_size 4 MB

cache_dir ufs /var/spool/squid 100 16 256

access_log /var/log/squid/access.log

acl localnet src 192.168.1.0/24

http_access allow localnet

http_access deny all

visible_hostname centossd

cache_mgr [email protected]

6.2 reload

reload 讓上面的配置生效。

# /etc/init.d/squid reload

6.3 添加iptables規則,把內部的http請求重新導向到3128連接埠

a) 啟動iptables服務

b) [[email protected] Desktop]# service iptables start

c) iptables: Applying firewall rules: [ OK ]

d) 清除現有規則

650) this.width=650;" title="clip_image022" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image022" src="http://s3.51cto.com/wyfs02/M00/6C/84/wKioL1VLJPmTLIQGAAC6dZcgIpM276.jpg" width="557" height="178" />

e) 儲存iptables 設定

650) this.width=650;" title="clip_image024" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image024" src="http://s3.51cto.com/wyfs02/M01/6C/84/wKioL1VLJPqDOmoiAABKX86jAow165.jpg" width="543" height="41" />

f) 查看nat表

650) this.width=650;" title="clip_image026" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image026" src="http://s3.51cto.com/wyfs02/M02/6C/84/wKioL1VLJPrhT52yAAB42kCxQ_Y005.jpg" width="558" height="130" />

g) 添加nat規則並儲存

配置iptables設定nat,即透明代理
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-ports 3128
如果只設定下面一句而上面一句不設定,用戶端也可以上網,只是不通過squid哈~
iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE

650) this.width=650;" title="clip_image028" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image028" src="http://s3.51cto.com/wyfs02/M00/6C/84/wKioL1VLJPuwyfioAAD1FmtJSvk332.jpg" width="558" height="233" />

儲存iptables設定
service iptables save

650) this.width=650;" title="clip_image030" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image030" src="http://s3.51cto.com/wyfs02/M01/6C/84/wKioL1VLJPvS5jhbAABA018Qxh4272.jpg" width="558" height="36" />

6.4 用戶端設定

1) 設定網路

650) this.width=650;" title="clip_image032" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image032" src="http://s3.51cto.com/wyfs02/M02/6C/84/wKioL1VLJPuhqG5XAADuen5TymI984.jpg" width="346" height="349" />

將網關設定為Proxy 伺服器內網地址

2) 在瀏覽器中將使用Proxy 伺服器選項勾除。

650) this.width=650;" title="clip_image033" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image033" src="http://s3.51cto.com/wyfs02/M00/6C/84/wKioL1VLJPzhJ9wqAAE17GI71vQ829.jpg" width="384" height="339" />

6.5 瀏覽器訪問測試

650) this.width=650;" title="clip_image035" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image035" src="http://s3.51cto.com/wyfs02/M01/6C/84/wKioL1VLJP3xGKodAAGWE7kf6zk438.jpg" width="557" height="341" />

說明透明代理設定成功

七、反向 Proxy服務

為Internet使用者訪問企業Web網站提供緩衝加速。

實驗拓撲圖如下:

650) this.width=650;" title="clip_image037" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image037" src="http://s3.51cto.com/wyfs02/M02/6C/88/wKiom1VLI4qhFF-yAABm89HeNS4164.jpg" width="558" height="249" />

內網伺服器是server 2008 R2

7.1 關閉內網伺服器防火牆

7.2 修改Web Server 首頁

建立目錄和首頁

650) this.width=650;" title="clip_image039" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image039" src="http://s3.51cto.com/wyfs02/M00/6C/88/wKiom1VLI4rQ_qxdAACt4iJSJPE388.jpg" width="558" height="262" />

7.3 配置squid

http_port 80 accel vhost //這個參數的用於支援主機映射

http_access allow all

cache_peer 192.168.1.118 parent 80 0 originserver round-robin weight=1 //指定第1台真實伺服器的位置

cache_peer 192.168.1.119 parent 80 0 originserver round-robin weight=1 //指定第2台真實伺服器的位置

visible_hostname centossd

7.4 啟動Squid服務

650) this.width=650;" title="clip_image041" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image041" src="http://s3.51cto.com/wyfs02/M01/6C/88/wKiom1VLI4qy5CSAAABcl4avnfc033.jpg" width="558" height="80" />

註:要確保80連接埠沒被使用

7.6 測試

squid 採用了round-robin,所以用戶端的訪問將輪詢兩台web伺服器,採用 "Ctrl + F5" 來深度重新整理測試。

web1

650) this.width=650;" title="clip_image043" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image043" src="http://s3.51cto.com/wyfs02/M02/6C/88/wKiom1VLI4uASsmyAABoc-ravKw903.jpg" width="558" height="142" />

web2

650) this.width=650;" title="clip_image045" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image045" src="http://s3.51cto.com/wyfs02/M00/6C/88/wKiom1VLI4uz2xNVAABl5m9Dr3U784.jpg" width="558" height="136" />

八、日誌

再來看看日誌資訊。,所有的行為都被日誌記載。

使用tail –f 可以即時監控日誌最後一行的變化。

650) this.width=650;" title="clip_image047" style="border-left- 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px" border="0" alt="clip_image047" src="http://s3.51cto.com/wyfs02/M01/6C/88/wKiom1VLI43wc9WpAAJV0nFf2C4360.jpg" width="557" height="313" />

 

 

註: 此文有參閱http://zpp2009.blog.51cto.com/730423/297023

http://bbs.51cto.com/thread-564760-1.html

http://www.cnblogs.com/mchina/p/3812190.html

三篇文章 ,感謝三位博主!

CENTOS 6.6 下squid 安裝配置

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.