ELB」カテゴリーアーカイブ




AWS ELB配下のApacheにて、アクセスログにアクセス元IPアドレスを正しく記録する方法




投稿日:

ELB配下にEC2インスタンスを設置する場合、接続元は送信元ではなくELBのアドレスになってしまいます。これではアクセス元の解析ができません。ELB自体にログも記録できるもの使い勝手の面もあります。

送信元のIPアドレスを記録する時に有名なモジュールが rpaf_module です。これを利用するのが良いかと思いましたが、近年のアップデートが全くないものですので、利用はしません。一応、試しに入れた記録は後半記載しています。

今回利用するのは、X-Forwarded-For です。 ELBまでhttps通信でELBとEC2インスタンスの間をhttpで通信している場合は、X-Forwarded-Proto に記録されます。よって、httpとhttpsがある場合には、両方設定します。


[html]
# vi /etc/httpd/conf/httpd.conf

LogFormat "%h %l %u %t %D \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %D %{X-Forwarded-For}i %{X-Forwarded-Proto}i" combined

httpだけなら以下でよいです。
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %D %{X-Forwarded-For}i" combined
[/html]



以下は、rpaf_moduleのインストール手順です。 これを書いている時点でメンテナンスはされていないようなのでおすすめしません。(ここで取得しているIPは仕組み上改ざんも簡単に出来るものなので、信頼はしてはいけません。)


[html]
# yum install gcc
# yum install httpd httpd-devel

gitからインストールしてみます。

# cd /usr/local/src
# git clone git://github.com/ttkzw/mod_rpaf-0.6.git
# cd mod_rpaf-0.6
# make
# sudo make install


———————————————————————-
Libraries have been installed in:
/usr/lib64/httpd/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR’
flag during linking and do at least one of the following:
– add LIBDIR to the `LD_LIBRARY_PATH’ environment variable
during execution
– add LIBDIR to the `LD_RUN_PATH’ environment variable
during linking
– use the `-Wl,-rpath -Wl,LIBDIR’ linker flag
– have your system administrator add LIBDIR to `/etc/ld.so.conf’

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
———————————————————————-
chmod 755 /usr/lib64/httpd/modules/mod_rpaf-2.0.so
[/html]
インストール完了です。


[html]
# vi /etc/httpd/conf.d/mod_rpaf.conf

LoadModule rpaf_module modules/mod_rpaf-2.0.so
RPAFenable On
RPAFsethostname On
RPAFproxy_ips 172.20.
RPAFheader X-Forwarded-For
[/html]


再起動して完了となります。

アクセスログをみると、IPが変換されているのが確認できます。