ロードバランス環境において、WEBサーバでクライアントユーザのIPを取得したいけど、通常の設定では上層であるロードバランサのIPを取得してしまいます。そんなお悩みを解決!
ロードバランサー → Nginxの場合
# ロードバランサIPを設定 set_real_ip_from 192.168.0.251; real_ip_header X-Forwarded-For;
Nginx構文チェック
# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Nginxの再起動
# /etc/init.d/nginx restart
これで大丈夫。
ロードバランサ → Nginx → Apache の場合
# wget https://raw.github.com/ttkzw/mod_rpaf-0.6/master/mod_rpaf-2.0.c # /usr/sbin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
mod_rpafモジュールが必要。これ以外にmod_extract_forwardedとかもあります。
# vi /etc/httpd/conf/httpd.conf
## mod_rpaf IP取得 ============================== LoadModule rpaf_module modules/mod_rpaf-2.0.so RPAFenable On RPAFsethostname On RPAFproxy_ips 127.0.0.1 RPAFheader X-Forwarded-For ## ==============================================
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
↓変更
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" common
# httpd -t Syntax OK
Apache再起動
# /etc/init.d/httpd restart
ログの確認
# tail -f /var/log/nginx/access.log
# tail -f /var/log/httpd/access.log
これで大丈夫٩(๑❛ᴗ❛๑)۶
