Squid

Squid是代理軟件,可緩存減低IO,可做正向代理(企業使用,降低寬帶使用率)、反向代理(網站靜態項緩存如圖片、流媒體等,用於網站架構)。

   一、Squid正向代理

   ##查看版本號

   squid -v

   #配置squid的配置文件,先清空,後加入

   echo “” > /etc/squid/squid.conf

   vim /etc/squid/squid.conf

加入代碼:

 

http_port 3128acl manager proto cache_objectacl localhost src 127.0.0.1/32 ::1acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1acl localnet src 10.0.0.0/8      # RFC1918 possible internal networkacl localnet src 172.16.0.0/12   # RFC1918 possible internal networkacl localnet src 192.168.0.0/16  # RFC1918 possible internal networkacl SSL_ports port 443acl Safe_ports port 80 8080         # httpacl Safe_ports port 21          # ftpacl Safe_ports port 443         # httpsacl CONNECT method CONNECThttp_access allow manager localhosthttp_access deny managerhttp_access deny !Safe_portshttp_access deny CONNECT !SSL_portshttp_access allow localnet    #與上面定義的localnet對應http_access allow localhost   #與上面定義的localhost對應http_access allow allcache_dir aufs /data/cache 1024 16 256cache_mem 128 MBhierarchy_stoplist cgi-bin ?coredump_dir /var/spool/squidrefresh_pattern ^ftp:           1440    20%     10080   ##配置緩存時間refresh_pattern ^gopher:        1440    0%      1440    ##配置緩存時間refresh_pattern -i (/cgi-bin/|\?) 0     0%      0refresh_pattern \.(jpg|png|gif|mp3|xml) 1440    50%     2880    ignore-reloadrefresh_pattern .               0       20%     4320

 

###http_port 3128” 指的是,squid服務啓動後將要監聽的端口,“cache_dir aufs”  這個用來指定本地磁盤上的緩存目錄,並制定緩存大小,以M爲單位,aufs爲緩存格式;cache_mem”它用來規定緩存佔用內存的大小;

 

##檢查配置文件是否錯誤,只有在啓動後才能使用

squid -kcheck

##初始化緩存目錄

mkdir /data/cache

hown -R squid:squid /data/cache/

squid -z

##啓動squid

/etc/init.d/squid start

##測試正向代理是否啓動成功

 curl -xlocalhost:3128  http://www.baidu.com/ -I

 

##重新加載配置文檔

squid -krec

 

##設置白名單,只允許白名單通過,其他不通過,在squid.conf中找到:

##acl CONNECT method CONNECT

##在其下面添加四行:

acl http proto HTTP

acl while_list dstdomain .yichuangshe.net.net

http_access allow http while_list

http_access deny http !while_list

##設置黑名單,跟白名單設置方法一樣:

acl http proto HTTP

acl black_list dstdomain .123.com

http_access allow http !black_list

http_access deny http black_list

##重新加載配置文檔,測試

squid -kche      ##測試配置文檔是否有問題

squid -krec       ##重新加載配置文檔

 

二、Squid反向代理

##修改配置文件,把端口修改爲80

http_port 80  accel vhost vport    ##這是必需添加的

##然後再增加你要代理的後端真實服務器信息:

cache_peer 14.17.32.211 parent 80 0 originserver name=a

cache_peer 180.97.33.107 parent 80 0 originserver name=b

cache_peer_domain a www.qq.com

cache_peer_domain b www.baidu.com

##其中cache_peer爲配置後端的服務器ip以及端口,name後邊爲要配置的域名,這裏和後面的cache_peer_domain相對應。實際的應用中,ip大多爲內外ip,而域名也許會有多個,如果是squid要代理一臺web上的所有域名,那麼就只寫一句即可:

cache_peer 120.24.98.198 parent 80 0 originserver

##反向代理主要用於緩存靜態項,因爲靜態項目尤其是圖片、流媒體等比較耗費帶寬,在中國,聯通網訪問電信的資源本例就慢,如果再去訪問大流量的圖片、流媒體那更會慢了,所以如果在聯通網配置一個squid反向代理,讓聯通客戶端直接訪問這個聯通squid,而這些靜態項已經被緩存在了squid上,這樣就大大加快了訪問速度。CDN, 其實它的設計原理就是這樣的思路。


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