25 KiB
title | author | date | url | wordtwit_post_info | categories | ||
---|---|---|---|---|---|---|---|
バックエンドにあるnginxのアクセスログにクライアントIPを記録する | kazu634 | 1969-12-31 | /1970/01/01/_46/ |
|
|
nginxでフロントエンドのプロキシーサーバと、バックエンドのウェブサーバを構成しています。今回、バックエンドのウェブサーバのログを確認したら、クライアントのIPアドレスが「127.0.0.1」となっており、クライアントのIPアドレスになっていないことに気づきました。そこでバックエンドサーバのアクセスログにクライアントのIPアドレスを記録する方法を調べてみました。
前提条件
フロントエンドのプロキシーサーバ側の設定で、X-Real-IPを設定している必要があります。nginx.confなどの設定ファイルで次のように設定しておきます。私はフロントエンドのnginxの設定部分に記載しています:
proxy_set_header X-Real-IP $remote_addr;
http_real_ipモジュール
調査の結果、HttpRealIpModuleがこの目的を満たすことがわかりました。HttpRealIpModuleとは、
This module allows to change the client’s IP address to value from request header (e. g. X-Real-IP or X-Forwarded-For).
プロキシーサーバのIPアドレスじゃなくて、X-Real-IPとか、X-Forwarded-Forを参照して、クライアントのIPアドレスとして扱ってくれるようになります。
設定方法
まずは http_real_ipモジュール が使用できるかどうかを確認します。次のコマンドを実行して、
% nginx -V nginx: nginx version: nginx/1..5 nginx: TLS SNI support enabled nginx: configure arguments: --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-debug --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module --add-module=/build/buildd/nginx-1.0.5/debian/modules/nginx-echo --add-module=/build/buildd/nginx-1.0.5/debian/modules/nginx-upstream-fair
configure argumentsの部分に「–with-http_realip_module」があれば、使用可能です。
バックエンド側に渡されてくるプロキシーサーバのIPアドレスは「127.0.0.1」で固定のため、バックエンド側の設定を実施しているnginx.confなどの設定ファイルで次のように設定します:
set_real_ip_from 127.0.0.1; real_ip_header X-Real-IP;
追記したら、設定を反映させましょう:
% sudo service nginx reload
結果
設定ファイルを変更する前:
127...1 blog.kazu634.com - [27/Aug/2011:22:52:03 +0900] "GET /wp-content/uploads/2011/08/slooProImg_20110815194429.jpg HTTP/1.0" 200 100647 "http://blog.kazu634.com/2011/08/15/%e5%b9%b3%e6%b3%89%e3%81%b8%e8%87%aa%e8%bb%a2%e8%bb%8a%e3%83%84%e3%83%bc%e3%83%aa%e3%83%b3%e3%82%b0/" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1" "-" 127...1 blog.kazu634.com - [27/Aug/2011:22:52:04 +0900] "GET /2011/08/13/%e5%a4%8f%e6%9c%9f%e4%bc%91%e6%9a%87%e3%81%ab%e8%a1%8c%e3%81%a3%e3%81%a6%e3%81%8d%e3%81%be%e3%81%99/ HTTP/1.0" 200 35155 "-" "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)" "-" 127...1 blog.kazu634.com - [27/Aug/2011:22:58:46 +0900] "POST /wp-cron.php?doing_wp_cron HTTP/1.0" 499 "-" "WordPress/3.0.1; http://www3232u.sakura.ne.jp/~kazu634" "-"
設定ファイルを変更した後:
119.63.196.125 - - [27/Aug/2011:23:47:41 +0900] "GET /2010/09/16/ハロウィーンですね/ HTTP/1.0" 200 35430 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"220.247.23.220 - - [27/Aug/2011:23:51:44 +0900] "GET /feed/ HTTP/1.0" 200 65789 "-" "Apple-PubSub/65.21" 66.249.69.54 - - [27/Aug/2011:23:56:33 +0900] "GET /page/10/ HTTP/1.0" 200 50757 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
参考
ちなみに私の設定ファイルはこんな感じにしています:
nginx.conf
% cat nginx.conf user www-data; worker_processes 2; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; # multi_accept on; } http { include /etc/nginx/mime.types; access_log /var/log/nginx/access.log; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 10; tcp_nodelay on; gzip on; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; proxy_cache_path /var/www/proxy/nginx_cache levels=1:2 keys_zone=czone:4m max_size=50m inactive=120m; proxy_temp_path /var/www/proxy/nginx_tmp; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } # mail { # # See sample authentication script at: # # http://wiki.nginx.org/NginxImapAuthenticateWithApachePhpScript # # # auth_http localhost/auth.php; # # pop3_capabilities "TOP" "USER"; # # imap_capabilities "IMAP4rev1" "UIDPLUS"; # # server { # listen localhost:110; # protocol pop3; # proxy on; # } # # server { # listen localhost:143; # protocol imap; # proxy on; # } # }
フロント・バックエンド側
% cat wordpress # You may add here your # server { # ... # } # statements for each of your virtual hosts server { listen 8001; server_name _; set_real_ip_from 127...1; real_ip_header X-Real-IP; access_log /var/log/nginx/blog.access.log; error_log /var/log/nginx/blog.error.log; location / { root /var/www/nginx-default/domain/wordpress; index index.php; if (-f $request_filename) { expires 30d; break; } if (!-e $request_filename) { rewrite ^.+?(wp-.*) $1 last; rewrite ^.+?(.*\.php)$ $1 last; rewrite ^ /index.php last; } } location ~ \.php$ { include /etc/nginx/fastcgi_params; fastcgi_pass 127...1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/nginx-default/domain/wordpress$fastcgi_script_name; } } upstream backend { ip_hash; server 127...1:8001; } server { listen 80; server_name blog.kazu634.com; access_log /var/log/nginx/front_proxy.access.log; error_log /var/log/nginx/front_proxy.error.log; location /wp-admin { proxy_pass http://backend; } location /wp-login.php { proxy_pass http://backend; } location / { if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) { set $do_not_cache 1; } if ($http_user_agent ~* “2.\ 2MMP|240×320|400X240|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine/3.|EudoraWeb|Googlebot-Mobile|hiptop|IEMobile|KYOCERA/WX310K|LG/U990|MIDP-2.|MMEF20|MOT-V|NetFront|Newt|Nintendo\ Wii|Nitro|Nokia|Opera\ Mini|Palm|PlayStation\ Portable|portalmmm|Proxinet|ProxiNet|SHARP-TQ-GX10|SHG-i900|Small|SonyEricsson|Symbian\ OS|SymbianOS|TS21i-10|UP.Browser|UP.Link|webOS|Windows\ CE|WinWAP|YahooSeeker/M1A1-R2D2|iPhone|iPod|Android|BlackBerry9530|LG-TU915\ Obigo|LGE\ VX|webOS|Nokia5800″) { set $do_not_cache 1; } proxy_no_cache $do_not_cache; proxy_cache_bypass $do_not_cache; proxy_pass http://backend; proxy_cache czone; proxy_cache_key $scheme$proxy_host$uri$is_args$args; proxy_cache_valid 200 10m; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { listen 80; server_name www3232u.sakura.ne.jp 59.106.177.26 localhost; access_log /var/log/nginx/sakura.access.log; error_log /var/log/nginx/sakura.error.log; location /~kazu634/ { rewrite ^.*/~kazu634(/.*) http://blog.kazu634.com$1; } location / { return 404; } location /nginx_status { stub_status on; access_log off; allow 127...1; deny all; } }