squid分流到tomcat,實現負載

測試環境:
兩Ip 192.168.1.1 和192.168.1.2
192.168.1.1(squid+tomcat)
192.168.1.2 (tomcat)
一、兩機器安裝tomcat,不詳細寫了
二、squid的安裝
   1.下載squid-2.7.STABLE9-20100914.tar.gz
   2.安裝  tar -xvzf squid-2.7.STABLE9-20100914.tar.gz
                cd squid-2.7.STABLE9-20100914
                ./configure --prefix=/usr/local/squid
                具體編譯參數可查看http://blog.wmdiy.com/?p=100
   3.配置
               vi /usr/local/squid/etc/squid.conf
             http_port 80 vhost vport
            visible_hostname text.com
            acl all src all
            acl manager proto cache_object
            acl localhost src 127.0.0.1/32
            #acl to_localhost dst 127.0.0.0/8 0.0.0.0/32
            acl localnet src 10.0.0.0/8     # RFC1918 possible internal network
            acl localnet src 172.16.0.0/12  # RFC1918 possible internal network
            acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
            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
             acl purge method PURGE
            http_access allow manager localhost
            http_access deny manager
            http_access deny !Safe_ports
            http_access deny CONNECT !SSL_ports
            http_access allow localnet
            http_access allow all
            icp_access allow localnet
            icp_access deny all
            http_access allow purge localhost
            http_access deny purge

           cache_mem 4096 MB
          #設置Squid所能使用的內存共400M
          cache_dir ufs /opt/cache 40960 16 256
          #磁盤緩存的類型和目錄,大小,一二級目錄的設置,這裏磁盤緩存大小是20G
           cache_swap_low 90
           cache_swap_high 95
           maximum_object_size 2000 KB
           #最大緩存文件大小,超過這個值則不緩存,這個值因人而異
           maximum_object_size_in_memory 128 KB
           #裝入內存緩存的文件大小,超過則不緩存
           cache_store_log none
           emulate_httpd_log on
           #打開emulate_httpd_log選項,將使Squid仿照Aapche的日誌格式
           logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
           acl QUERY urlpath_regex cgi-bin
           acl ddcache urlpath_regex \getb**.jsp\?
           no_cache allow ddcache
           no_cache deny QUERY

           cache_peer 127.0.0.1 parent 8080  0 no-query no-digest   originserver   name=www1   round-robin
           cache_peer 192.168.1.2 parent 8080 0 no-query no-digest   originserver   name=www2   round-robin
           cache_peer_domain www1 www.xxx.com
           cache_peer_domain www2 www.xxx.com
           cache_peer_access www1 allow all
           cache_peer_access www2 allow all
           cache_effective_user squid
          cache_effective_group squid
         #squid使用的用戶組和用戶名

         hierarchy_stoplist cgi-bin ?
        access_log /usr/local/squid/var/logs/access.log squid

        refresh_pattern ^ftp:           1440    20%     10080
        refresh_pattern ^gopher:        1440    0%      1440
        refresh_pattern -i (/cgi-bin/|\?) 0     0%      0
        refresh_pattern .               0       20%     4320

        refresh_pattern -i searchNP\?  1440     25%    10080 override-expire override-lastmod reload-into-ims ignore-reload  ignore-no-cache ignore-private
        refresh_pattern -i \.jsp\?  1440     25%    10080 override-expire override-lastmod reload-into-ims ignore-reload  ignore-no-cache ignore-private
       refresh_pattern -i \.gif$   1440    50%     2880      ignore-reload 
       refresh_pattern -i \.jpg$   1440    50%     2880      ignore-reload
       refresh_pattern -i \.jpeg$  1440    50%     2880     ignore-reload
       refresh_pattern -i \.png$   1440    50%     2880      ignore-reload

      acl shoutcast rep_header X-HTTP09-First-Line ^ICY.[0-9]
      upgrade_http0.9 deny shoutcast
      acl apache rep_header Server ^Apache
      broken_vary_encoding allow apache
      coredump_dir /usr/local/squid/var/cache
      (這個可以參考同目錄下的squid.conf.default)

三、啓動squid
   1.squid -k parse
      看不到輸出,配置文件有效,你能繼續後面的步驟。然而,如果配置文件包含錯誤,squid會有提示
   2.初始化cache目錄.(對自己定義的cache目錄,squid用戶要有讀寫權限,系統要有squid用戶)
      squid -z
   3.啓動 (之前要對存放log的目錄squid有讀寫權限,否則squid不會啓動)
      squid -s  (調試可用 squid -Nd1)
    基本配置完成
四、測試 http://www.xxx.com/index.jsp
     把默認安裝的兩個tomcat中的index.jsp稍作不同,可以在訪問時看到兩個頁面在輪循

squid的功能是委強大的 ,還有很多全命令,如下:
    *取得squid運行狀態信息: squidclient -p 80 mgr:info
    *取得squid內存使用情況: squidclient -p 80 mgr:mem
    *取得squid已經緩存的列表: squidclient -p 80 mgr:objects. use it carefully,it may crash
    *取得squid的磁盤使用情況: squidclient -p 80 mgr:diskd
     *強制更新某個url:squidclient -p 80 -m PURGE http://www.php-oa.com/static.php
     *更多的請查看:squidclient -h 或者 squidclient -p 80 mgr:

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章