Sometimes, your web application is installed in a web server which is behind reverse proxy server like Cloudflare or other services. In that condition, your application will always read the reverse proxy server IP as client IP. In Nginx, you can use
ngx_http_realip_module
to get the real client IP (see detail).To enable this module, you need to make sure that you use latest Nginx server from repository or you can build it from source with
--with-http_realip_module
configuration parameter. I prefer to install it from repository as Nginx 1.10.0 in Ubuntu 16.04 has enabled this module by default. You can check whether it's enabled or not by following command.$ nginx -V
Then, the reslut should be like:
$ nginx version: nginx/1.10.0 (Ubuntu) ... ... --with-http_realip_module ... ...
Now, you can put the module configuration in http block or server block of your Nginx configuration. For example, you use Cloudflare service for your web application. You should list all available Cloudflare IP ranges. The configuration might be like this following script.
server {
...
...
# list all Cloudflare IP ranges
...
set_real_ip_from 173.245.48.0/20; set_real_ip_from 188.114.96.0/20; ...
set_real_ip_from 2400:cb00::/32; set_real_ip_from 2606:4700::/32;
...
# use any of the following two
# Cloudflare specific real_ip_header CF-Connecting-IP;
# Generic #real_ip_header X-Forwarded-For; ...
... }
Comments
Post a Comment