squid+apache實現緩衝加速

來源:互聯網
上載者:User

標籤:squid   緩衝   squid+apache   緩衝最佳化   

本執行個體是squid和apache在同一台機器上,squid做前端反向 Proxy,連接埠為80,apache作為後端web,連接埠為81

伺服器ip:172.16.8.102

1.首先介紹下版本選擇,在進行測試之前一定要選定一個合適的squid版本,在此推薦2.7,她和2.6功能相似但更好的支援http1.1,也有3.0以上版本的不少特性。

2.squid2.7安裝

cd /usr/local/src

tar -zxvf squid-2.7.STABLE9.tar.gz

cd squid-2.7.STABLE9

 ./configure -prefix=/usr/local/squid2.7 -enable-xmalloc-statistics --enable-async-io=320 --with-maxfd=65536 -enable-useragent-log -enable-referer-log -enable-epoll -disable-poll -enable-large-cache-files -disable-internal-dns -enable-linux-netfilter -enable-truncate -enable-x-accelerator-vary -enable-follow-x-forwarded-for -with-large-files -with-pthreads -enable-storeio="aufs,coss,diskd,ufs" -enable-kill-parent-hack -enable-gnuregex -enable-cache-digests -enable-delay-pools -enable-stacktraces -enable-default-err-language=Simplify_Chinese -enable-err-languages="Simplify_Chinese English" --enable-auth="basic" --enable-basic-auth-helpers="NCSA" --enable-snmp

make && make install

3.建立suqid使用者

useradd squid

4.建立緩目錄

cd /data

mkdir -p squid/cache

chown -R squid.squid squid

5.建立日誌目錄

 cd /var/log

mkdir cache


5.配置squid.conf

cd /usr/local/squid2.7

vim squid.conf

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 81 # http
acl Safe_ports port 3128 # http
acl Safe_ports port 8080 # 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
http_access allow manager localhost localnet
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow all
icp_access allow localnet
icp_access deny all
http_port 80 accel vhost vport
cache_peer 127.0.0.1 parent 81 0 no-query originserver name=test
cache_peer_access test allow all
hierarchy_stoplist cgi-bin ?
cache_mem 1024 MB
maximum_object_size_in_memory 6 MB
memory_replacement_policy lru
cache_replacement_policy lru
cache_dir ufs /data/squid/cache 1024 16 256
maximum_object_size 6 MB
cache_swap_low 90
cache_swap_high 95
access_log /var/log/squid/access.log
cache_log /var/log/squid/cache.log 
refresh_pattern ^ftp: 144020%10080
refresh_pattern ^gopher: 14400%1440
refresh_pattern -i (/cgi-bin/|\?) 0 0%0
refresh_pattern \.(jpg|png|gif|mp3|xml|html|htm|css|js|aspx) 1440    50%     2880    ignore-reload
refresh_pattern . 020%4320
acl shoutcast rep_header X-HTTP09-First-Line ^ICY.[0-9]
cache_vary on
acl apache rep_header Server ^Apache
broken_vary_encoding allow all
cache_effective_user squid
cache_effective_group squid
visible_hostname 172.16.8.102 
icp_port 0
reload_into_ims on
coredump_dir /usr/local/squid2.7/var/cache


所更改的參數解釋:

(1)acl Safe_ports port 81 # http
     acl Safe_ports port 3128 # http
     acl Safe_ports port 8080 # http

此處定義可以訪問的連接埠,由於http_access deny !Safe_ports,只要不是在Safe_ports中出現的連接埠都會被限制,這個可以根據實際情況而定.

(2)http_access allow all

在此我定義的是所有的ip都可以訪問squid,這也是為了方便我在測試環境中使用,如果是線上應用請制定相應的訪問限制。

(3)http_port 80 accel vhost vport

定義訪問squid的連接埠。

如果不加accel vhost vport說明你的squid預設做為一個快取服務器,這個時候如果用戶端有請求發到了squid,squid起到的是路由功能,把請求轉寄出去,被真正的web server接收,web server返迴響應,當squid接收到響應後,根據回應標頭決定是否緩衝,此時的squid,只是一個cache server。

如果加上accel vhost vport說明你的squidsquid就從一個緩衝(cache server)變成了一個web server, 這個時候squid在80連接埠監聽請求,同時和web server的請求連接埠(vhost vport)綁定,這個時候請求到了squid,squid是不用轉寄請求的,而是直接要麼從緩衝中拿資料要麼向綁定的連接埠直接請求資料。另外綁定連接埠還有一個好處,可以充分利用http 回應標頭中的到期時間頭和etag頭。 

     cache_peer 127.0.0.1 parent 81 0 no-query originserver name=test

反向 Proxy81連接埠,81連接埠為apache;no-query不做查詢,直接擷取資料;orginserver 代表是原始伺服器;name定義反向 Proxy的名字,可以對acl控制

(4)cache_mem 1024 MB

設定所用記憶體的大小
maximum_object_size_in_memory 6 MB

設定緩衝對象所佔用的最大記憶體
memory_replacement_policy lru

cache_replacement_policy lru

替換機制
cache_dir ufs /data/squid/cache 1024 16 256

緩衝目錄的大小,應該不低於cache_mem
maximum_object_size 6 MB

最大的單個緩衝對象

(5)access_log /var/log/squid/access.log
cache_log /var/log/squid/cache.log 

設定squid的日誌目錄,注意日誌許可權,否則有可能導致squid無法啟動

(6)refresh_pattern \.(jpg|png|gif|mp3|xml|html|htm|css|js|aspx) 1440    50%     2880    ignore-reload

設定jpg等尾碼格式的檔案在cache中停留的時間

(7)cache_vary on

如果你發現squid快取命中率很低,即使調整refresh_pattern,maximum_object_size_in_memory,加大記憶體都沒用;利用cachemgr.cgi統計工具中的In-Memory and In-Transit Objects,發現HTML/js/css not_in_memory,而jpg/png等圖片都緩衝了,則可能是有由於這個參數off導致。

這是因為apache在 response header 中返回了一個vary:Accept-encoding ,則squid在儲存快取檔案時需要將“瀏覽器”request header 資訊中的Accept-encoding欄位的值(gzip,deflate之類)作為緩衝key的一部分,因此對於不同的Accept-encoding欄位值,都需要儲存不同的檔案。(IE與firefox的要求標頭的Accept-encoding欄位值中就有一個空格的差別下次

請求到squid的時候,需要先找到一個快取檔案的索引檔案,根據索引檔案中的不同的Accep-encoding值再去找相應的快取檔案。 cache vary off,那麼經過gzip壓縮後含有vary頭的,都不會被cache了,所以和上述緩衝策略沒什麼影響,而jpg本來是被壓縮過,不含vary,自然會被cache了。

(8)cache_effective_user squid
cache_effective_group squid

設定squid的使用者和組

(9)icp_port 0

禁用icp鄰居,如果你想使用squid叢集可以更改這個參數

(10)reload_into_ims on

 開啟這個全域參數,可將用戶端發來的no-cache轉化為If-Modified-Since去處理

這個參數的設定,可以參考此部落格http://blog.sina.com.cn/s/blog_56d8ea9001018xev.html

(11)hierarchy_stoplist cgi-bin ?

此為預設參數,任何包含問號或cgi-bin字串的請求匹配該列表,變成不可層疊。

 Squid內在的將每個用戶端請求標記為層疊或不可層疊。不可層疊的請求看起來不會導致cache命中。例如,POST請求的響應幾乎從不會被cache。在squid能簡單的串連到原始伺服器時,轉寄不可cache目標的請求到鄰居cache,純粹是浪費資源。
某些區分層疊和不可層疊請求的規則,在squid裡難於編碼。例如,POST和PUT方式總是不可層疊的。然而,hierarchy_stoplist指令允許你定製這種演算法。它包含一個字串列表,當在URI裡發現它們時,squid將請求標記為不可層疊。


更改完設定檔後,可以進行初始化緩衝目錄和啟動squid了

/usr/local/squid2.7/sbin/squid -z

/usr/local/squid2.7/sbin/squid 

lsof -i:80

COMMAND  PID  USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
squid   1399 squid   17u  IPv4 9965038      0t0  TCP *:http (LISTEN)

說明啟動成功,如果發現啟動沒成功,則檢查設定檔

6.cachemgr.cgi統計工具

vim /usr/local/squid2.7/etc/cachemgr.conf

localhost:80

80連接埠為squid的http_port連接埠

cd /var/www/html

mkdir squid/cgi-bin

cp /usr/local/squid2.7/libexec/cachemgr.cgi /var/www/html/squid/cgi-bin

在apache中設定相應的訪問

vim /etc/httpd/conf.d/squid.conf

ScriptAlias /squid/cgi-bin/cachemgr.cgi /usr/local/squid2.7/libexec/cachemgr.cgi

# Only allow access from localhost by default
<Location /squid/cgi-bin/cachemgr.cgi>
 order allow,deny
# allow from localhost.localdomain
 allow from all
 # Add additional allowed hosts as needed
 # allow from .example.com
</Location>

service httpd restart使設定檔生效。

由於Apache使用的是81連接埠,我們直接用81連接埠訪問即可

http://172.16.8.102:81/squid/cgi-bin/cachemgr.cgi

由於我們沒有設定使用者名稱密碼直接存取即可,但是應用到線上則必須設定。

7.apache配置

網站的訪問配置我在直接用的是我們一個測試網站,在這不做過多介紹。但在此要介紹下apache的mod_expoires模組,此模組可以減少10%左右的重複請求,讓重複的使用者對指定的頁面請求結果都CACHE在本地,根本不向伺服器發出請求。

檢查apache按安裝有mod_expires模組,因此我們只需要在/etc/httpd/conf.d/mod_expires.conf中進行配置即可。

vim /etc/httpd/conf.d/mod_expires.conf

<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 12 hours"
ExpiresByType text/html "access plus 3 days"
ExpiresByType text/plain "access plus 3 days"
ExpiresByType text/css  "access plus 7 days"
ExpiresByType image/gif "access plus 30 days"
ExpiresByType image/png "access plus 30 days"
ExpiresByType image/jpeg "access plus 30 days"
ExpiresByType image/jpg "access plus 30 days"
ExpiresByType image/x-icon "access plus 30 days"
ExpiresByType video/x-flv  "access plus 30 days"
ExpiresByType application/x-shockwave-flash "access plus 30 days"
</IfModule>

其中對所有檔案可以緩衝的檔案都預設設定為12小時,對text/image/video等類型的檔案重新設定成相應的緩衝時間。

設定完成後service httpd restart 即可。


最後我們訪問測試,然後查看快取命中了。



squid+apache實現緩衝加速

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.