squid緩存代理服務基本配置

1.squid三種代理特點:

squid普通代理:
需要客戶機在瀏覽器中指定代理服務器的地址、端口。

squid透明代理:
適用於企業的網關主機;
客戶機不需要指定代理服務器地址、端口等信息;
通過iptables將客戶機的web訪問數據轉交給代理服務程序處理。

squid反向代理:
爲internet用戶訪問企業web站點提供緩存加速。

2.安裝:
源碼:
tar xf squid-3.3.5.tar.bz2 && cd squid-3.3.5
./configure ‘–prefix=/usr/local/squid’ ‘–enable-gnuregex’ ‘–enable-dlmalloc’ ‘–enable-debug-cbdata’ ‘–enable-async-io=100′ ‘–with-pthreads’ ‘–enable-storeio=aufs,diskd,ufs’ ‘–enable-removal-policies=heap,lru’ ‘–enable-icmp’ ‘–enable-delay-pools’ ‘–enable-useragent-log’ ‘–enable-referer-log’ ‘–disable-wccp’ ‘–disable-wccpv2′ ‘–enable-kill-parent-hack’ ‘–enable-arp-acl’ ‘–enable-snmp’ ‘–enable-default-err-language=Simplify_Chinese’ ‘–enable-err-languages=Simplify_Chinese English’ ‘–disable-poll’ ‘–enable-epoll’ ‘–disable-ident-lookups’ ‘–disable-internal-dns’ ‘–enable-truncate’ ‘–enable-underscores’ ‘–enable-basic-auth-helpers=NCSA’ ‘–enable-stacktrace’ ‘–with-winbind-auth-challenge’ ‘–enable-large-cache-files’ ‘–with-large-files’ ‘–with-maxfd=65535′ ‘–enable-x-accelerator-vary’ –enable-ltdl-convenience && make && make install
啓動/關閉:/usr/local/squid/sbin/squid start|stop
重載配置:/usr/local/squid/sbin/squid -k reconfigure

yum:
yum install -y squid
啓動/關閉:service squid start|stop
重載配置:squid -k reconfigure

3.配置信息:
交換空間設定選項
cache_dir ufs /var/spool/squid 100M 16 256 #設置緩存數據時使用的交換空間參數,總大小爲100M,16個目錄,每個目錄下有256個子目錄,可以指定多次
cache_swap_low 90
cache_swap_high 95
說明:squid使用大量的交換空間來存儲對象。那麼,過了一定的時間以後,該交換空間就會用完,所以還必須定期的按照某種指標來將低於某個水平 線的對象清除。squid使用所謂的“最近最少使用算法”(LRU)來做這一工作。當已使用的交換空間達到cache_swap_high時,squid 就根據LRU所計算的得到每個對象的值將低於某個水平線的對象清除。這種清除工作一直進行直到已用空間達到cache_swap_low。這兩個值用百分 比表示,如果你所使用的交換空間很大的話,建議你減少這兩個值得差距,因爲這時一個百分點就可能是幾百兆空間,這勢必影響squid的性能。
maximum_object_size 4096KB #允許保存到高速緩存的最大文件大小
cache_mem 64MB #設置用於緩存功能的內存空間大小,一般爲物理內存的1/4-1/3
reply_body_max_size 10240000 allow all #允許用戶下載的最大文件大小,默認單位爲字節

日誌的選項
cache_access_log /usr/local/squid/var/logs/cache_access.log combined #指定客戶請求記錄日誌的完整路徑
cache_store_log /usr/local/squid/var/logs/cache_store.log #指定對象存儲記錄日誌的完整路徑,該記錄表明哪些對象被寫到交換空間,哪些對象被從交換空間清除。
cache_log /var/log/squid/cache.log 指定squid一般信息日誌的完整路徑
access_log /var/log/squid/access.log squid #指定日誌文件的保存位置和記錄格式
用戶訪問控制選項
request_size 100KB 設置用戶請求通訊量的最大允許值(單位爲KB)。如果用戶用POST方法請求時,應該設一個較大的值
reference_age
說明:squid根據對象的LRU(最近最少使用算法)來清除對象,squid依據使用磁盤空間的總量動態地計算對象的LRU年齡。我們用 reference_age定義對象的最大LRU年齡。如果一個對象在指定的reference_age內沒有被訪問,squid將刪除該對象。缺省值爲 一個月。你可以使用如下所示的時間表示方法。
1 week
3.5 days
4 months
2.2 hours

管理參數選項
cache_mgr root 設置管理員郵件地址
cache_effective_user squid #啓動squid的用戶
cache_effective_group squid #啓動squid的組
visible_hostname proxy.benet.com #設置代理服務器可用的完整主機名
unique_hostname 如果你有一個代理服務器陣列,並且你爲每個代理服務器指定了同樣的“visible_hostname”,同時你必須爲它們指定不同的“unique_hostname”來避免“forwarding loops ”(傳輸循環)發生

其他選項
http_port 3128 #指定代理服務監聽的地址和端口
dns_testnames www.google.com #測試squid開機啓動後是否能正常工作
memory_pools on|off #如果設置爲on,squid將保留所有已經分配(但未使用)的內存池以便在將來使用。
log_icp_queries on|off #是否對icp請求作日誌,如果系統負載過大,可以off,默認爲on

4.ACL訪問控制:
ACL,訪問控制列表,可以從客戶機的ip地址、請求訪問的url、域名、文件類型、訪問時間、併發請求數等各方面進行控制。
定義acl列表:acl 列表名稱 列表類型 列表內容
限制acl列表:http_access allow或deny 列表名稱
eg:acl all src 0.0.0.0/0.0.0.0
http_access deny all
列表類型:
src 源ip eg:acl lan1 src 192.168.1.0/24
dst 目的ip
port 端口
srcdomain 源域名
dstdomain 目的域名 eg:acl blk_domain dstdomain .qq.com .kaixin001.com
time 時間 eg:acl work_hours time MTWHF 08:30-17:30
maxconn 最大連接 eg:acl max20_conn maxconn 20
url_regex url正則過濾 eg:acl blk_url url_regex -i ^rtsp:// ^mms://
urlpath_regex urlpath正則過濾 eg :acl realfile urlpath_regex -i \.rmvb$ \.rm$ sex
acl列表訪問控制:(acl列表可以聯合)
http_access deny lan1 blk_url (拒絕lan1的ip訪問^rtsp:// ^mms://)
acl訪問權限
http_access allow|deny 列表名

5.實驗:
squid 傳統代理
基本配置:
yum -y install squid*
vim /etc/squid/squid.conf
http_port 192.168.1.77:3128 #指定服務端口
visible_hostname 192.168.1.77 #報錯時顯示服務器的名稱
acl innet src 192.168.1.0/24 #名稱爲innet的ip羣體
acl all src 0.0.0.0/0.0.0.0 #其它ip羣體
http_access allow innet #允許innet羣體
http_access deny all #拒絕all羣體

squid 透明代理
基本配置:
acl all src 0.0.0.0/0.0.0.0
http_port 網關內網IP:3128 transparent
http_access allow all

iptables -t nat -I PREROUTING -i eth0 -p tcp –dport 80 -j REDIRECT –to-ports 3128

squid 反向代理
基本配置:
http_port 200.168.10.1:80 accel
cache_peer 192.168.10.2 parent 80 0 originserver weight=5 max-conn=30
cache_peer 192.168.10.3 parent 80 0 originserver weight=5 max-conn=30
cache_peer 192.168.10.4 parent 80 0 originserver weight=5 max-conn=30
cache_peer 192.168.10.5 parent 80 0 originserver weight=5 max-conn=30
#cache_peer server地址 服務器類型 http端口 icp端口 [可選項]
visible_hostname 200.168.10.1
acl all src 0.0.0.0/0.0.0.0
http_access allow all

反向代理虛擬主機映射:
http_port 200.168.10.1:80 accel
cache_peer 192.168.10.2 parent 80 0 originserver name=a
cache_peer 192.168.10.3 parent 80 0 originserver name=b
cache_peer_domain a www.lamp.com
cache_peer_domain b www.web.com
cache_peer_access a allow all
cache_peer_access b allow all

6.緩存查看:
通過查看web服務器的訪問日誌,狀態爲304表示,通過緩存
代理服務器下通過/var/spool/aquid/ 下tree命令,查看文件數,如果有緩存文件,則通過head 00000000X 查看緩存數據。
客戶端通過firefox的firebug插件來查看請求和響應頭信息,如果是從緩存服務器取得數據,則可以看到squid信息。


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