歡迎您光臨本站 註冊首頁

Squid反向加速實戰篇!

←手機掃碼閱讀     火星人 @ 2014-03-04 , reply:0

Squid反向加速實戰篇!

Squid網站加速技術文檔

Squid是一種用來緩衝Internet數據的軟體。它是這樣實現其功能的,接受來自人們需要下載的目標(object)的請求並適當地處理這些請求。
也就是說,如果一個人想下載一web頁面,他請求Squid為他取得這個頁面。Squid隨之連接到遠程伺服器(比如:http://www.abc.cn/)並向這個頁面發出請求。然後,Squid顯式地聚集數據到客戶端機器,而且同時複製一份。當下一次有人需要同一頁面時,Squid可以簡單地從磁碟中讀到它,那樣數據迅即就會傳輸到客戶機上。
當前的Squid可以處理HTTP,FTP,GOPHER,SSL和WAIS等協議。但它不能處理如POP,NNTP,RealAudio以及其它類型的東西。
因此,對於網站靜態頁面加速,訪問速度有明顯的提高。目前比較盛行的CDN伺服器其實也就是採用Squid反向加速技術。
下面是搭建Squid反向代理(加速)的技術步驟:
獲取Squid
    Squid是完全開源的軟體,因此直接可以在官網上下載源碼,然後再linux下進行編譯安裝。
    download site: http://www.squid-cache.org/Download
編譯和安裝
    下載完squid后,放在linux目錄下,解壓,然後編譯安裝。
# cd /tmp
# tar xzvf squid-3.0.STABLE13.tar.gz
# cd squid-3.0.STABLE13
# ./configure --enable-cachemgr-hostname=192.168.1.15 --prefix=/usr/local/squid
# make && make install
編譯安裝完成。
配置
   安裝完成後,進入 /usr/local/squid目錄
# ls –l 會發現有下面幾個子目錄

./bin  目錄包含對所有用戶可用的程序;
./etc  下面是是Squid的配置文件和 cachemgr.cgi配置文件;
./libexec libexec 目錄傳統的包含了輔助程序;
./sbin  目錄下的程序正常只能被root 啟動,是Squid主程序;
./share 目錄通常包括squid 的只讀數據文件;
./var 目錄包含了不是很重要的和經常變化的文件。
        ./ var/cache 假如你不在squid.conf 文件里指定,這是默認的緩存目錄(cache_dir)
     重點在 /etc/squid.conf 配置:
       # cat squid.conf
visible_hostname www.abc.cn
http_port 80 vhost vport
cache_mem 1024 MB
maximum_object_size_in_memory 2048 KB
memory_replacement_policy lru
cache_dir ufs /usr/local/squid/var/cache 10240 32 256
max_open_disk_fds 0
request_timeout 120 seconds
client_lifetime 360 minutes
half_closed_clients off
pconn_timeout 120 seconds
visible_hostname virtual3
client_db off
ipcache_size 10240
ipcache_low 90
ipcache_high 95
check_hostnames off
cache_swap_low 90
cache_swap_high 95
maximum_object_size 10240 KB
maximum_object_size_in_memory 256 KB
maximum_object_size 32768 KB
cache_log /dev/null
cache_store_log none
request_header_max_size 10 KB
request_body_max_size 102400 KB
read_timeout 15 minutes
access_log /usr/local/squid/var/logs/access.log
#cache_log /usr/local/squid/var/logs/cache_webcache.log
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
access_log /usr/local/squid/var/logs/page_zs_access_log combined
pid_filename /usr/local/squid/var/logs/squid.pid
###############################
cache_peer 192.168.1.10 parent 80 0 no-query no-digest originserver name=www
cache_peer xxx.xxx.xxx.99 parent 80 0 no-query no-digest originserver name=www
cache_peer xxx.xxx.xxx.101 parent 80 0 no-query no-digest originserver name=test1
cache_peer_domain www www.abc.cn
cache_peer_domain test1 test1.abc.cn
################################
cache_peer_access www allow all
cache_peer_access test1 allow all
http_access allow all
acl manager proto cache_object
acl QUERY urlpath_regex cgi-bin .jsp .php .cgi .avi .wmv .rm .ram .mpg .mpeg .zip .exe
refresh_pattern -i \.html$                         15      90%     120
refresh_pattern -i \.htm$                         15      90%     120
refresh_pattern -i \.js$                           15      90%     120
refresh_pattern -i index(\d*)\.shtml$                5      90%      10
refresh_pattern -i \.shtml$                        15      90%     120
refresh_pattern -i \.shtm$                        15      90%      120
refresh_pattern -i \.jpg$                          120     90%     2880
refresh_pattern -i \.gif$                          120     90%     2880
refresh_pattern -i \.jpeg$                         120     90%     2880
refresh_pattern -i \.png$                         120     90%     2880
refresh_pattern -i \.php$                         0        0%      0
refresh_pattern -i      \.wma$    1440   90%    14400   ignore-reload
refresh_pattern -i      \.zip$    1440    90%     14400   ignore-reload
refresh_pattern -i      \.mp3$   1440    90%     14400   ignore-reload
refresh_pattern -i      \.flv$    1440    90%     14400   ignore-reload
refresh_pattern -i      \.rar$    1440    90%     14400   ignore-reload
refresh_pattern -i      \.rm$    1440    90%     14400   ignore-reload
refresh_pattern -i      \.avi$    1440    90%     14400   ignore-reload
refresh_pattern -i      \.3gp$   1440    90%     14400   ignore-reload
refresh_pattern -i      \.mp4$   1440    90%     14400   ignore-reload
cache deny QUERY
acl manager proto cache_object
acl inside src 192.168.1.0/24
http_access allow manager
http_access deny manager !inside
cachemgr_passwd 123456 all
生成cache目錄;
/usr/local/squid/sbin/squid –z
查看:


啟動測試看是否能正常啟動:
Shell下直接運行: /usr/local/squid/sbin/squid
然後# ps ax 看進程是否起來

netstat –nlp 查看80埠(配置文件里設置)是否起來,如果80埠起來,運行成功,同時需要訪問測試。看squid是否真正啟作用。
# netstat  -nlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address  Foreign Address   State  PID/Program name
tcp        0      0 0.0.0.0:80      0.0.0.0:*      LISTEN  21991/(squid)   
tcp        0      0 :::81          :::*           LISTEN  21716/httpd   
tcp        0      0 :::22          :::*           LISTEN  1800/sshd   
udp        0      0 0.0.0.0:32773  0.0.0.0:*                21991/(squid)

Cachemgr.cgi配置
    為了更方便檢查命中率,還需要配置squid自帶的cgi腳本: cachemgr.cgi ,次腳本可以放在本機的apache或其他機器的apache /cgi-bin/目錄下,然後進行配置:
注意: squid 的etc目錄下的 cachemgr.conf 和 cachemgr.cgi 文件必須放在同一個目錄下。
修改 cachemgr.conf 目錄下的配置為
192.168.1.15:80  此處埠80為squid埠。否則後面會報錯。
配置apache
1、去掉 AddHandler cgi-script .cgi 前面的 # 號;
2、ScriptAlias /cgi-bin/ "/usr/local/apache/cgi-bin/"  去掉前面的 『#』,一般apache默認是去掉的;
3、增加一下配置:

<Directory "/usr/local/apache/cgi-bin">
  Options FollowSymLinks
      AllowOverride None
      Order allow,deny
      Allow from all
      Options -Indexes
</Directory>

<Location /cgi-bin/cachemgr.cgi>
AuthType Basic
AuthName "squidadmin"
AuthUserFile /usr/local/squid/etc/squid.pwd
require valid-user
</Location>
   
    生成cachemgr.cgi訪問密碼:
       htpasswd -c squid.pwd squidadmin
       chown apache.apache squid.pwd
    重啟apache 和 squid
    /usr/local/apache/bin/apachectl restart
    /usr/local/squid/sbin/squid
訪問cachemgr.cgi


     輸入帳號密碼:
     
        通過可以查看squid伺服器cache的命中率。


完:em10:

[火星人 ] Squid反向加速實戰篇!已經有633次圍觀

http://coctec.com/docs/service/show-post-24327.html