--> -->

skimemo


skimemo - 日記/2017-07-23

_ apache2にmod_bwを入れて最大接続数を制限する

訳あってapacheにmod_bwを入れたのですが、何故か苦労したのでメモメモ(._.)φ

_ インストール

_ apxsを使う方法

OSはCentOS7なのですが、ググると出てくるapxsを使う方法は何故かこんなエラーが出るのであっさり断念。

$ sudo apxs -i -a -c mod_bw.c
/usr/lib64/apr-1/build/libtool --silent --mode=compile gcc -std=gnu99 -prefer-pic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic  -DLINUX -D_REENTRANT -D_GNU_SOURCE -pthread -I/usr/include/httpd  -I/usr/include/apr-1   -I/usr/include/apr-1   -c -o mod_bw.lo mod_bw.c && touch mod_bw.slo
mod_bw.c: In function 'get_bw_rate':
mod_bw.c:484:59: error: 'conn_rec' has no member named 'remote_addr'
             if (apr_ipsubnet_test(e[i].x.ip, r->connection->remote_addr)) {
                                                           ^
mod_bw.c: In function 'get_maxconn':
mod_bw.c:567:59: error: 'conn_rec' has no member named 'remote_addr'
             if (apr_ipsubnet_test(e[i].x.ip, r->connection->remote_addr)) {
                                                           ^
mod_bw.c: In function 'get_sid':
mod_bw.c:612:59: error: 'conn_rec' has no member named 'remote_addr'
             if (apr_ipsubnet_test(e[i].x.ip, r->connection->remote_addr)) {
                                                           ^
mod_bw.c: In function 'update_counters':
mod_bw.c:673:9: warning: implicit declaration of function 'apr_atomic_cas' [-Wimplicit-function-declaration]
         if (apr_atomic_cas32(&bwstat->lock, 1, 0) == 0) {
         ^
mod_bw.c:687:13: warning: implicit declaration of function 'apr_atomic_set' [-Wimplicit-function-declaration]
             apr_atomic_set32(&bwstat->lock, 0);
             ^
mod_bw.c: In function 'bw_filter':
mod_bw.c:818:5: warning: implicit declaration of function 'apr_atomic_inc' [-Wimplicit-function-declaration]
     apr_atomic_inc32(&bwmaxconn->connection_count);
     ^
mod_bw.c:821:5: warning: format '%ld' expects argument of type 'long int', but argument 8 has type 'int' [-Wformat=]
     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
     ^
mod_bw.c:843:13: warning: implicit declaration of function 'apr_atomic_dec' [-Wimplicit-function-declaration]
             apr_atomic_dec32(&bwmaxconn->connection_count);
             ^
mod_bw.c:902:17: warning: implicit declaration of function 'apr_atomic_add' [-Wimplicit-function-declaration]
                 apr_atomic_add32(&bwstat->bytes_count, packet);
                 ^
mod_bw.c: At top level:
mod_bw.c:462:12: warning: 'is_ip' defined but not used [-Wunused-function]
 static int is_ip(const char *host)
            ^
apxs:Error: Command failed with rc=65536

_ yumを使う方法

一方でrpmを指定してyumでインストールする方法もあるようです。
結果的にこちらで入れました。

$ sudo yum install http://repo.unmanarc.com/CentOS/7/RPMS/x86_64/mod_bw-0.92-2.el7.centos.x86_64.rpm

どこに入ったか確認。

$ rpm -ql mod_bw
/etc/httpd/conf.d/mod_bw.conf
/usr/lib64/httpd/modules/mod_bw.so
/usr/share/doc/mod_bw-0.92
/usr/share/doc/mod_bw-0.92/ChangeLog
/usr/share/doc/mod_bw-0.92/LICENSE
/usr/share/doc/mod_bw-0.92/TODO
/usr/share/doc/mod_bw-0.92/mod_bw.txt

_ 設定

_ mod_bw.conf

以下の設定ファイルを編集します。

/etc/httpd/conf.d/mod_bw.conf

ひな形を除いて書いた内容だけ抜粋するとこんな感じにしました。
各パラメータの意味は参考文献参照。

LoadModule bw_module    modules/mod_bw.so

<IfModule mod_bw.c>
 BandWidthModule On
 ForceBandWidthModule On
 BandWidthError 510
 BandWidth    all 102400000
 MaxConnection all 100
</IfModule>

_ httpd.conf

ErrorDocument 510 "Max connection reached. Please retry later."

_ 苦労したところ

・ForceBandWidthModuleはデフォルトoffですが、Onにしないと全部のリクエストに効かないので、Onを指定する必要があります。
・BandWidthは0を指定するとMaxConnectionが有効にならないという噂があり、大きな値を指定しています。
・またこのBandWidthの指定値の単位がよく分からないのですが、あまり大きな値を入れるとそれはそれで全体が無効になるようです。
・BandWidthの指定をDirectoryディレクティブに書いてみましたがうまく有効にできませんでした。なのでmod_bw側に全部書いています。
・ErrorDocument 510はDirectoryディレクティブに書いても効かないようです。サーバー全体に関するエラーだからですかね。

あとこれはapacheの設定の問題だと思いますが、ErrorDocumentにローカルファイルを指定しても、ファイルが見つからないのか正しく反映されませんでした。DocumentRootなど色々試しましたが同じでした。これってどこを指してるんでしょう??

_ 参考文献

http://www.bitbull.ch/notes/post/bandwith-limitation-with-apache-on-centos-7/
http://www.aconus.com/~oyaji/tips/apache_tips4.htm

Category: [Linux] - 16:13:40



 
Last-modified: 2017-07-23 (日) 16:13:42 (2461d)