squid配置 反向代理

Squidhtml

      功能:save on their internet web traffic, improve performance ,deliver faster browsting to therir end-clients and provide static,dynamic and siredming content to millions of internet usersworldwideweb

      特色:offers a rich access controlsql

 代理:瀏覽器

正向代理:1、普通代理服務緩存

   即標準的、傳統的代理服務 ;須要客戶機在瀏覽器中指定代理服務器的地址、端口 服務器

2、透明代理服務dom

適用於企業的網關主機(共享接入Internet)中客戶機不須要指定代理服務器地址、端口等信息;須要設置防火牆策略將客戶機的Web訪問數據轉交給代理服務程序處理 tcp

     反向代理: 1、反向代理服務:ide

            緩存加速對象:FTP, HTTP ,gopher Not dns測試

           協議:ICP(Internet Cache Protocol)  端口3130/UDP

反向代理服務 Internet用戶訪問企業Web站點提供緩存加速  ;對用戶的Web訪問進行過濾控制

 

反向代理軟件:SquidVarnishATSNginx

官網:www.squid-cache.org

有個squid命令的介紹:

 squid –k parse 語法測試

 squid –zX 磁盤驅動器的速度

 squid –z 若是新增長了cache_dir是須要用此命令,該命令會在其下建立子目錄

 squid –sD 服務的啓動 也能夠用service squid restart重啓

 squid –k shutdown 關閉服務

 squid –k routate 回滾日誌

普通代理服務典型應用環境:

配置Squid 

         安裝: yum install squid –y

有關於主配置文件的內容經常使用選項的介紹:

http_port 3128   這個是squid的監聽的端口號,默認ip地址0.0.0.0也能夠改爲監聽某個或者某段IP:例:http_port 192.168.0.0:3128

  
  
           
  
  
  1. visible_hostname www.a.org 這個If you want to present a special hostname in error messages, etc,  
  2.  
  3.    define this. Otherwise, the return value of gethostname()  
  4.  
  5.    will be used. If you have multiple caches in a cluster and 
  6.  
  7.    get errors about IP-forwarding you must set them to have individual  
  8.  
  9.    names with this setting.  
  10.  
  11. # reply_body_max_size 0 allow all 訪問請求容許最大字節是0,這個本身能夠視狀況更改  
  12.  
  13. access_log /var/log/squid/access.log squid 日誌文件位置  
  14.  
  15. maximum_object_size 4096 KB 訪問對象最大值,若是超過這個設定值則squid拒絕  
  16.  
  17. cache_mem 64M 緩存內存是64M,這個值通常是內存的三分之一就好  
  18.  
  19. cache_dir ufs /var/spool/squid 100 16 256 緩存目錄格式是ufs 其餘格式有「disk」 ,」aufs」, 「ufs」,「100」 是最大是100M, 「16」是緩存一級子目錄個數,「256」是緩存二級子目錄個數  
  20.  
  21. host_access 定義訪問控制,通常與acl結合使用  
  22.  
  23. http_access allow localhost 如若初始配置,是隻容許localhost訪問的,如要能代理其餘IP,只要加上個host_access就好了 

   ACL訪問控制介紹:

      格式: acl 名字 類型 內容

             httpd_access alldeny 名字   這裏的名字和ACL的名字是一致的方能生效

  acl中類型有這幾種:src ,dst, port ,srcdomain, dstdomain, time, maxcom, usrl_regex,urlpath_regex

   acl裏內容就本身定義:

 

 

有關ACL的實例:

  
  
           
  
  
  1. acl LAN1 src 192.168.0.0/24  
  2. acl PC1 SRC 172.16.0.1/32  
  3.  
  4. http_access deny LAN1 PC1 

 訪問控制的匹配順序:沒有設置任何規則時,則拒絕全部客戶端訪問請求;有規則但找不到是將採用最後一天規程相反的權限,好比定義的是allow,那對客戶端的請求就是deny 

透明代理: 要用透明代理前提條件是:客戶機的Web訪問數據要能通過防火牆;

代理服務構建在網關(防火牆)主機中

      工做機制就是squidiptables結合其餘,經過iptables將源iP訪問的請求指定到squid的端口上,不須要讓客戶端在IE中配置代理服務和端口省去了麻煩

   實現步驟:

  
  
           
  
  
  1. 修改squid.conf文件:  
  2.  
  3.         httpd_port 192.168.0.1:3128 transport  
  4.  
  5.    添加iptables:  
  6.  
  7.  iptables -t nat -I PREROUTING -i eth1 -s 192.168.0.0/24 -p tcp --dport 80 -j REDIRECT --to-ports 3128 
  
  
           
  
  
  1. 區分nat轉換,這個不過是將源IP轉換成公網IP訪問外網,而透明代理是經過代理服務代理訪問,另有nat轉換命令  
  2.  
  3. iptables –t nat –A POSTROUTING –S 192.168.0.0/24 –j SNAT –to-source 10.0.0.1 

     反向代理配置:

    實現配置:修改squid.conf文件

  
  
           
  
  
  1. http_port 192.168.0.1:80 vhost  
  2.  
  3.      cache_peer 192.168.0.1 parent 80 0 originserver weight=5 max-conn=30 

保存就好了。

配置介紹:

    格式:cache_peer WEB服務地址 服務器類型 http端口 icp 端口 其餘選項

cache_peer中定義的是若是本地沒有資源的話去尋找定義IP中尋找資源

服務器類型:’parent’ ,’sibling’, multicast’

Http(proxy-port);  緩存代理請求監聽的端口號

icp-port : 用於查詢有關對象的鄰居緩存

其餘選項:weight=n 定義權重,max-conn=n 最大鏈接數, originserver 在加速器中使用用於和父服務器聯繫

反向代理實例:

原本應該在加上個DNS 服務器,因爲機器限制虛擬機開不了那麼多機器只好講DNS捨棄,webserver1webserver2上安裝Apache服務,squid1能代理webserver1不能代理webserver2, squid2能代理webserver2不代理webserver1 

 

 

 

配置:

準備工做:/etc/hosts文件加入如下內容:

  
  
           
  
  
  1. 172.16.0.100 squid1.a.org squid1  
  2.  
  3. 172.16.0.200 squid2.a.org squid2 

    在webserver12上安裝Apache服務,能夠用yum安裝,也能夠編譯安裝

  
  
           
  
  
  1. #yum install httpd  
  2.  
  3. #service httpd start  
  4.  
  5. #chkconfig httpd on 

分別在webserver12作內不一的網頁

配置squid服務;squid1

      #yum install squid –y

 編輯/etc/squid/squid.conf文件:

  
  
           
  
  
  1. http_access allow all 
  2.  
  3.    http_port 80 accel vhost vport  
  4.  
  5.    icp_port 3130  
  6.  
  7. ##################squid中沒有資源找鄰居###########################  
  8.  
  9. cache_peer squitd1.a.org sibling 80 3130  
  10.  
  11. cache_peer squitd2.a.org sibling 80 3130  
  12.  
  13. #############################################  
  14.  
  15. cache_peer 172.16.0.111 parent 8080 0 originserver max-conn=30 weight=10  
  16.  
  17. cache_peer 172.16.0.134 parent 8080 0 originserver max-conn=20 weight=20  
  18.  
  19. ###############################################  
  20.  
  21. cache_log /var/logs/cache.log  
  22.  
  23. cache_dir ufs /var/spool/squid 100 16 256  
  24.  
  25. visible_hostname squitd1.a.org 

    查看日誌

  
  
           
  
  
  1. [root@server68 squid]# tail /var/log/squid/cache.log   
  2.  
  3. 2011/09/06 16:55:16| 0 Objects expired.  
  4.  
  5. 2011/09/06 16:55:16| 0 Objects cancelled.  
  6.  
  7. 2011/09/06 16:55:16| 0 Duplicate URLs purged.  
  8.  
  9. 2011/09/06 16:55:16| 0 Swapfile clashes avoided.  
  10.  
  11. 2011/09/06 16:55:16| Took 0.4 seconds ( 0.0 objects/sec).  
  12.  
  13. 2011/09/06 16:55:16| Beginning Validation Procedure 
  14.  
  15. 2011/09/06 16:55:16| Completed Validation Procedure 
  16.  
  17. 2011/09/06 16:55:16| Validated 0 Entries  
  18.  
  19. 2011/09/06 16:55:16| store_swap_size = 0k  
  20.  
  21. 2011/09/06 16:55:17| storeLateRelease: released 0 objects 

在客戶端訪問就好了,squidwebserver中任意一個斷掉都不影響訪問

日誌查看:

  
  
           
  
  
  1. tail access.log   
  2.  
  3. 1312362413.806 423 172.16.0.99 TCP_CLIENT_REFRESH_MISS/304 279 GET http://172.16.0.134:8080/ - FIRST_UP_PARENT/172.16.0.134 -  
  4.  
  5. 1312362414.279 473 172.16.0.99 TCP_CLIENT_REFRESH_MISS/304 279 GET http://172.16.0.134:8080/ - FIRST_UP_PARENT/172.16.0.134 -  
  6.  
  7. 1312362414.469 45 172.16.0.99 TCP_CLIENT_REFRESH_MISS/304 279 GET http://172.16.0.134:8080/ - FIRST_UP_PARENT/172.16.0.134 -  
  8.  
  9. 1312362414.668 199 172.16.0.99 TCP_CLIENT_REFRESH_MISS/304 279 GET http://172.16.0.134:8080/ - FIRST_UP_PARENT/172.16.0.134 -  
  10.  
  11. 1312362498.349 17080 172.16.0.99 TCP_CLIENT_REFRESH_MISS/200 403 GET http://172.16.0.111/ - DIRECT/172.16.0.111 text/html  
  12.  
  13. 1312362506.945 30142 172.16.0.99 TCP_CLIENT_REFRESH_MISS/503 1437 GET http://172.16.0.134:8080/ - DIRECT/172.16.0.134 text/html  
  14.  
  15. 1312362559.980 5189 172.16.0.99 TCP_MISS/200 404 GET http://172.16.0.111:8080/ - DIRECT/172.16.0.111 text/html  
  16.  
  17. 1312362562.860 29 172.16.0.99 TCP_CLIENT_REFRESH_MISS/304 281 GET http://172.16.0.111:8080/ - FIRST_UP_PARENT/172.16.0.111 -  
  18.  
  19. 1312362566.273 2 172.16.0.99 TCP_CLIENT_REFRESH_MISS/200 403 GET http://172.16.0.134:8080/ - FIRST_UP_PARENT/172.16.0.111 text/html  
  20.  
  21. 1312362567.765 1395 172.16.0.99 TCP_CLIENT_REFRESH_MISS/304 281 GET http://172.16.0.134:8080/ - FIRST_UP_PARENT/172.16.0.111 – 

     這是代理的配置以及效果圖

 

完成!

相關文章
相關標籤/搜索