天天看點

Squid 代理服務的配置與應用

squid是一個基于http1.0(目前幾乎可以和1.1相容)的全功能的代理伺服器

功能:1 共享網絡

2 加快通路速度,節約通信帶寬

3 防止内部主機受到攻擊

4 限制使用者通路,完善網絡管理

主要實作網絡通路加速的功能。

<a href="http://lyp0909.blog.51cto.com/attachment/201103/21/508999_1300710435GZYn.gif"></a>

1 用戶端A向代理伺服器提出通路Internet的請求;

2 代理伺服器接收到請求後,首先與通路控制清單中的通路規則相對照,如果滿足規則,在在緩存中查找是否存在需要的資訊。

3 如果緩存中存在用戶端A需要的資訊,則将資訊傳送給用戶端。如果不存在,代理伺服器就代替用戶端向Internet上的主機請求指定的資訊;

4 Internet上的主機将代理伺服器的請求資訊發送到代理伺服器中,同時代理服務會将資訊存入到緩存中;

5 代理伺服器将Internet上主機的回應資訊傳送給用戶端A;

6 用戶端B向代理伺服器提出相同的請求;

7 代理伺服器也首先與通路控制清單中的通路規則相對照;

8 如果滿足,則将緩存中的資訊傳送給用戶端B。

普通代理服務

即标準的、傳統的代理服務

需要客戶機在浏覽器中指定代理伺服器的位址、端口

透明代理服務

适用于企業的網關主機(共享接入Internet)中

客戶機不需要指定代理伺服器位址、端口等資訊

需要設定防火牆政策将客戶機的Web通路資料轉交給代理服務程式處理

ICP protocol 網際網路緩存協定,用于在代理伺服器之間交換緩存,使用UDP協定3130端口。

Proxy:Squid,Varnish,ATS,Nginx

傳統代理,即正向代理,類似于SNAT

反向代理 類似于DNAT,實作反向代理加速。

CDN:内容分發網路 Content Delievery Network

<a href="http://lyp0909.blog.51cto.com/attachment/201103/21/508999_1300710436N1bF.gif"></a>

反向代理

Squid是開源界比較流行的應用層代理伺服器,具有權限管理靈活,性能高和效率快等特點。

squid軟體包

軟體包名:squid-2.6.STABLE6

服務名:squid

主程式:/usr/sbin/squid

配置目錄:/etc/squid/

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

預設監聽端口:TCP 3128

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

squid的主配置檔案:

[root@station39 ~]# cat /etc/squid/squid.conf | grep -v "^#" | grep -v "^$"

acl all src 0.0.0.0/0.0.0.0

acl manager proto cache_object

acl localhost src 127.0.0.1/255.255.255.255

acl to_localhost dst 127.0.0.0/8

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

http_access allow manager localhost

http_access deny manager

http_access deny !Safe_ports

http_access deny CONNECT !SSL_ports

http_access allow localhost

http_access deny all

icp_access allow all

http_port 3128 //**監聽内網網卡

hierarchy_stoplist cgi-bin ?

access_log /var/log/squid/access.log squid

acl QUERY urlpath_regex cgi-bin \?

cache deny QUERY

refresh_pattern ^ftp: 1440 20% 10080

refresh_pattern ^gopher: 1440 0% 1440

refresh_pattern . 0 20% 4320

acl apache rep_header Server ^Apache

broken_vary_encoding allow apache

coredump_dir /var/spool/squid

幾個常用配置項:

# cache_mem 8 MB //**代理程序所使用的記憶體的大小

# maximum_object_size_in_memory 8 KB //**最大緩存對象

#reply_body_max_size 10240000 allow all //**請求的最大檔案大小

#access_log /var/log/squid/access.log squid //**通路日志存放位置

#visible_hostname proxy.test.com //**可見主機名

#cache_dir ufs /var/spool/squid 100 16 256 //**指定緩存目錄 ufs:所使用的檔案系統,

比較快,适合存放小檔案;緩存目錄;100:緩存目錄可是使用的

緩存空間的大小;16:一級緩存子目錄的個數; 256:二級緩存子

目錄的個數

下面我們來模拟使用squid代理上網的場景:

<a href="http://lyp0909.blog.51cto.com/attachment/201103/21/508999_1300710437Un7W.gif"></a>

代理伺服器eth0網卡192.168.0.39使用代理可以上網。是以我們假設這裡是公網。目的是區域網路内的PC機通過代理伺服器來上網。

我們來配置代理伺服器:

修改/etc/squid/squid.conf主配置檔案

[root@station39 ~]# vim /etc/squid/squid.conf

http_port 192.168.10.11:3128

visible_hostname proxy.a.com

cache_mem 128 MB

cache_dir ufs /var/spool/squid 1024 16 256

error_directory /usr/share/squid/errors/Simplify_Chinese //**将錯誤日志改為中文

http_access allow all //**修改預設通路政策

檢查文法:

[root@station39 ~]# squid -k parse

初始化緩存

[root@station39 ~]# squid -z

2011/03/10 19:13:39| Creating Swap Directories

[root@station39 ~]# service squid start

[root@station39 ~]# netstat -ntlp | grep 3128

tcp 0 0 192.168.10.11:3128 0.0.0.0:* LISTEN 13129/(squid)

添加網關:

[root@station39 ~]# route add default gw 192.168.0.254 //**由于我們這裡是内網位址,所

以需要通過指定網關的方式來上網

用戶端需要在浏覽器中設定使用代理通路:

<a href="http://lyp0909.blog.51cto.com/attachment/201103/21/508999_1300710437Ocs2.gif"></a>

看!已經可以通路外網了!

<a href="http://lyp0909.blog.51cto.com/attachment/201103/21/508999_13007104387gT0.gif"></a>

當勾選use this proxy server for all protocols 時,我們就可以通過代理伺服器來通路所有的服務,包括ftp。

<a href="http://lyp0909.blog.51cto.com/attachment/201103/21/508999_1300710440XuJt.gif"></a>

<a href="http://lyp0909.blog.51cto.com/attachment/201103/21/508999_1300710440kQW2.gif"></a>

幾個常用功能的使用:

reply_body_max_size 10240000 allow all //限制使用者所能通路的最大資源 line 780

acl 通路控制清單

基于源位址的通路控制

允許來自192.168.10.0/24網段能夠通路外網

先定義acl

acl LAN src 192.168.10.0/24 //** line 628

再使用

http_access allow LAN //** line 635

修改預設政策

http_access deny all //**line 639

<a href="http://lyp0909.blog.51cto.com/attachment/201103/21/508999_13007104410oYP.gif"></a>

基于目标位址的通路控制

禁止所有人通路QQ

acl QQ dstdomain .qq.com

http_access deny QQ

<a href="http://lyp0909.blog.51cto.com/attachment/201103/21/508999_1300710442ORvF.gif"></a>

禁止192.168.10.0/24通路QQ

acl LAN src 192.168.10.0/24

http_access allow LAN !QQ

<a href="http://lyp0909.blog.51cto.com/attachment/201103/21/508999_13007104426Do5.gif"></a>

基于url_regex的通路控制

acl GUGE url_regex -i ^http://www.google.com.hk/index.html

http_access allow LAN !GUGE

<a href="http://lyp0909.blog.51cto.com/attachment/201103/21/508999_1300710443AlfE.gif"></a>

通過url拒絕對*.iso的通路

acl NOTISO url_regex -i ^.*tp://.*\.iso$

http_access allow LAN !NOTISO

<a href="http://lyp0909.blog.51cto.com/attachment/201103/21/508999_1300710444HhAc.gif"></a>

根據時間來做控制:

定義時間段:

acl WORKTIME time MTWHF 08:00-18:00

http_access allow LAN !NOTISO WORKTIME

<a href="http://lyp0909.blog.51cto.com/attachment/201103/21/508999_1300710445lMLn.gif"></a>

PS:當兩個清單一樣時,将以兩個清單的并集作為參數的值。

透明代理

透明代理是NAT和代理的完美結合,在這種工作方式下使用者感覺不到代理伺服器的存在。當客戶通路Internet時,請求資料包經過Linux伺服器轉發時,linux伺服器上的iptables将客戶機的HTTP請求重定向到Squid代理伺服器,由代理伺服器代理客戶機通路外部資訊資源,再将擷取的資料傳回客戶機。

修改squid的主配置檔案

http_port 192.168.10.11:8080 transparent //**實作透明代理

添加一條iptables規則:

iptables -t nat -A PREROUTING -i eth1 -s 192.168.10.0/24 -p tcp --dport 80 -j REDIRECT --to-ports 8080

這樣用戶端依舊無法上網,因為用戶端的DNS請求無法發送出去,怎麼辦?

我們需要再添加一條規則,使内網的DNS請求能夠出去

iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -j SNAT --to-source 192.168.0.127

打開核心路由功能:

echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward

也許這裡由會有疑問了,使用源位址轉換直接就可以上網了,那我們的資料還通過squid伺服器不?答案是肯定的,因為SNAT是在POSTROUTING鍊上做的,而我們做透明代理的話是在PREROUTING鍊上做的,資料包在進入伺服器的時候已經被重定向到squid上去了,是以無論如何基于80端口的通路是繞不開squid的。

用戶端指定網關:

route add default gw 192.168.10.11

反向代理 reverse proxy

我們來假設一個反向代理的場景

<a href="http://lyp0909.blog.51cto.com/attachment/201103/21/508999_1300710446Nu6O.gif"></a>

在虛拟機裡建構實驗環境:

192.168.10.1 192.168.10.11 僅主機 我們假設這是外網

192.168.0.127 192.168.0.254 橋接 我們假設這是一個網站的内部架構

最終目的是讓PC機通過squid代理來通路web 服務。

配置squid伺服器:

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

http_port 192.168.10.11:80 vhost //** line 919

cache_peer 192.168.0.254 parent 80 0 originserver weight=1 max-conn=1000

//** line 1449

http_access allow all //** line 637 修改ACL

儲存退出。

清除緩存

[root@station39 squid]# rm -rf /var/spool/squid/*

重建緩存

[root@station39 squid]# squid -z

2011/03/11 15:52:38| Creating Swap Directories

文法檢查

[root@station39 squid]# squid -k parse

重新開機服務。

我們在用戶端使用firefox通路一下192.168.10.11

<a href="http://lyp0909.blog.51cto.com/attachment/201103/21/508999_1300710447Qhs2.gif"></a>

看!已經可以通路了。這就是反向代理。

本文轉自 490999122 51CTO部落格,原文連結:http://blog.51cto.com/lyp0909/521613,如需轉載請自行聯系原作者