Blog信息 |
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7613429 建立时间:2006年5月29日 |

| |
[网站架构]Apache 限制流量 软件技术
lhwork 发表于 2006/12/27 15:12:16 |
這篇由永遠學不會所提問的問題,老闆問他在 Apache 裡有沒有辦法限制每個 user 的流量限制 ? 若問我的話一定掛在那,這個問題我倒是從來沒有想過 ~ 今晚就來實作一下在 Apache 上要做到流量限制的套件有 mod_bandwidth 、 mod_throttle 與 mod_cband ~前二個適用於 Apache 1.3.x 版本,不過我升級到 Apache 2.x 很久了,所以前二個套件就不研究了。取而代之在 Apache 2 可以 work 的套件就是 mod_cband引用一下 The FreeBSD Ports Archive mod_cband 這篇文章:mod_cband
is an Apache 2 module provided to solve the problem of limiting
virtualhosts bandwidth usage. When the configured virtualhost’s
transfer limit is exceeded, mod_cband will redirect all further
requests to a location specified in the configuration file.大概了解一下後,就可以開始來安裝套件 ~連
結上 mod_cband 的網站,目前最新的版本是 0.9.7.4 STABLE。Downalod 下來後解開,可以稍微看一下 INTSALL
來安裝,蠻簡單的 ~ 由於我的 Apache 一直都是以 tarball 的方式來安裝,所以這裡也用 tarball 的方式來安裝囉,或許用
ports 來安裝更快吧 :pShell> tar zxvf mod-cband-0.9.7.4.tgzShell> cd mod-cband-0.9.7.4Shell> ./configure –with-apxs=/www/bin/apxsShell> make;make install;make clean好了之後可以檢查一下 httpd.conf 有沒有多一行LoadModule cband_module modules/mod_cband.so重開 Apache 後再利用 phpinfo 去檢查一下有沒有載入 ~接下來是設定的部份,可參考 Manage Apache Download Speed And Traffic Limits With mod_cband 這篇文章喔 !! 利用網站的範例來做解釋好了 :p假設目前我有一個 URL 叫 www.bojack.com,IP 是 1.2.3.4,在電腦裡的檔案路徑為 /var/www首先我們先在 httpd.conf 加入二行設定,可以用來改善 mod_cband 的效能# mod_cbandCBandScoreFlushPeriod 1CBandRandomPulse On另外我們可以先增加一個目錄用來計算 mod_cband 所使用的情況,這部稍後會說明,還蠻有趣的mkdir /var/www/scoreboardchown apache:apache /var/www/scoreboard/Example 1 :<VirtualHost 1.2.3.4>ServerName www.bojack.comServerAdmin bojack@bojack.comDocumentRoot /var/wwwCBandSpeed 1024 10 30CBandRemoteSpeed 20kb/s 3 3</VirtualHost>CBandSpeed 的部份是指所有總連線速率最大為1024kbps,每秒最多只能有10個連線,Total的連線數最大為30。CBandRemoteSpeed是限制每一個獨立的連線速率最大為20kbps,限制来自远端访问速度20kB每秒,3个请求每秒,3个连接。
設定好之後就可以重開 Apache 來測試一下 ~ 我放了一個 5mb 的檔案下載,嗯,果然傳輸速度會慢慢被限制在 20 k 左右http://blog.bojack.idv.tw/my_files/apache_limit_1.gifExample 2.<VirtualHost 1.2.3.4>ServerName www.bojack.comServerAdmin bojack@bojack.comDocumentRoot /var/wwwCBandLimit 100MCBandExceededSpeed 128 5 15CBandScoreboard /var/www/scoreboardCBandPeriod 4W</VirtualHost>這
個範例就有趣多了,是限制 www.bojack.com 這個站每個月的流量限製為 100MB,當超出這個流量後,就限製為 128
kbps,相對的 max requests per second 變成 5,max open connections 變為
15,還記得一開始要求建立 scoreboard 這個目錄吧,原來它就是用來計算用的!至於 CbandPeriod 4W
就是指四個星期,就是一個月啦!Example 3.<VirtualHost 1.2.3.4>ServerName www.bojack.comServerAdmin webmaster@bojack.comDocumentRoot /var/wwwCBandLimit 100MCBandExceededURL http://www.bojack.com/traffic_exceeded.htmlCBandScoreboard /var/www/scoreboardCBandPeriod 4W</VirtualHost>這個範例看起來和上面那個很像,不過就是當流量超過 100MB 時,就會顯示 traffic_exceeded.html 通知使用者說你這個流量已超出限制了 ~ 了解吧Example 4.<VirtualHost 1.2.3.4>ServerName www.bojack.comServerAdmin bojack@bojack.comDocumentRoot /var/wwwCBandLimit 100MCBandExceededSpeed 128 5 15CBandScoreboard /var/www/scoreboardCBandPeriod 4W<Location /cband-status>SetHandler cband-status</Location><Location /cband-status-me>SetHandler cband-status-me</Location></VirtualHost>這個範例可以讓使用者稍微了解一下自己所使用的流量情況囉 ~ 以這範例為例,鍵入 http://www.bojack.com/cband-status 或 http://www.bojack.com/cband-status-me 即可http://blog.bojack.idv.tw/my_files/apache_limit_2.gif (http://blog.bojack.idv.tw/my_files/apache_limit_2_all.gif)[1] mod_cband (http://cband.linux.pl/)[2] 讓 mod_cband 在某些條件下不設限 (http://phorum.study-area.org/viewtopic.php?t=37634)[3]
Manage Apache Download Speed And Traffic Limits With mod_cband
(http://www.howtoforge.com/mod_cband_apache2_bandwidth_quota_throttling)[4] [問題]Apache 有辦法限制流量嗎? (http://phorum.study-area.org/viewtopic.php?p=204260#204260)[5] apache 限制下載流量與連線數 (http://blog.webs-tv.net/lisans/article/2232922)[6] mod_bandwidth (http://www.cohprog.com/mod_bandwidth.html) |
|
|