LoginSignup
1

More than 5 years have passed since last update.

posted at

updated at

CloudFlare経由でのNginxが受け取るIPを正しいものにする方法

■ 起因

Webサーバを構築する際にCDNを使って、サーバの負荷を下げる際にCloudFlareを使うことがあると思うのですがの際、CloudFlare→Nginxを経由するとアクセスログのIPがすべてCloudFlareのIPになってしまい、正しいリクエストIPがわからなくなってしまいます。その際の解決策について、この記事で説明します。

■ 解決策

2つ行うことはあります。

ステップ1

今回は、NginxのHttpRealipModuleを利用します。そのため、まずNginxにHttpRealipModuleの「--with-http_realip_module」が入っているか確認します。

$ nginx -V
e.g
$ nginx -V
nginx version: nginx/1.13.12
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2o  27 Mar 2018
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-http_image_filter_module --with-http_geoip_module --with-http_perl_module --add-module=./extensions/ngx_cache_purge --add-module=./extensions/nginx-ct --add-module=./extensions/ngx_brotli --add-module=./extensions/passenger/src/nginx_module --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -I/usr/local/openssl/include' --with-ld-opt=-L/usr/local/openssl/lib

ステップ2

ステップ1で、HttpRealipModuleの「--with-http_realip_module」が入っていることが確認できたら、NginxのconfにCloudFlareのIPを記載していきます。

nginx.conf
    # Cloudflare
    set_real_ip_from 103.21.244.0/22; 
    set_real_ip_from 103.22.200.0/22; 
    set_real_ip_from 103.31.4.0/22; 
    set_real_ip_from 104.16.0.0/12; 
    set_real_ip_from 108.162.192.0/18; 
    set_real_ip_from 131.0.72.0/22; 
    set_real_ip_from 141.101.64.0/18; 
    set_real_ip_from 162.158.0.0/15;
    set_real_ip_from 172.64.0.0/13; 
    set_real_ip_from 173.245.48.0/20; 
    set_real_ip_from 188.114.96.0/20; 
    set_real_ip_from 190.93.240.0/20; 
    set_real_ip_from 197.234.240.0/22; 
    set_real_ip_from 198.41.128.0/17; 
    set_real_ip_from 2400:cb00::/32; 
    set_real_ip_from 2600:4700::/32; 
    set_real_ip_from 2803:f800::/32; 
    set_real_ip_from 2405:b500::/32;
    set_real_ip_from 2405:8100::/32; 
    set_real_ip_from 2c0f:f248::/32; 
    set_real_ip_from 2a06:98c0::/29; 
    real_ip_header CF-Connecting-IP;

1点注意点として上記のCloudFlareのIPは変わるので下記のページに記載があるIPを適用するようにしてください。
https://www.cloudflare.com/ips/

参考 : https://support.cloudflare.com/hc/en-us/articles/200170706-How-do-I-restore-original-visitor-IP-with-Nginx-

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
1