Setting CodeIgniter behind a proxy server

What happens if you do not have a list of proxy addresses? When using website acceleration services such as Akamai, Google, Edgecast or CloudFront; it would always be difficult to get the IP addresses for the sessions from them.

When testing our codeigniter application on CDNs, we noticed that IP addresses were passed as CDN IPs, not client IPs in the session database.

How can you get around this?

/* |-------------------------------------------------------------------------- | Reverse Proxy IPs |-------------------------------------------------------------------------- | | If your server is behind a reverse proxy, you must whitelist the proxy IP | addresses from which CodeIgniter should trust the HTTP_X_FORWARDED_FOR | header in order to properly identify the visitor IP address. | Comma-delimited, eg '10.0.1.200,10.0.1.201' | */ $config['proxy_ips'] = ''; 

Thanks!

+6
source share
2 answers

Depending on the web server you are using, there are modules that will give you the true client IP address from the HTTP_X_FORWARDED_FOR header or the equivalent for your proxy.

I use Nginx on my web server for CloudFlare, so I use this: http://wiki.nginx.org/HttpRealipModule

You need to configure the module for your web server, which receives the real IP address of the client from the correct header from your CDN.

+4
source

If you want to handle the IP address in Codeigniter 3, but not on the web server, you can add the IP CDN to this configuration:

 $config['proxy_ips'] = ['10.0.1.2']; 

or assigning a CDN network mask:

 $config['proxy_ips'] = '10.0.1.0/24'; 

If your server is always behind the CDN:

 $config['proxy_ips'] = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : ''; 

Get the IP method:

 $this->input->ip_address(); //$this=>CI 

It will return the IP address if the proxy IP address matches, otherwise it will return $_SERVER["REMOTE_ADDR"] , which can get the real IP address of the client.

Referring to Codeigniter 3

0
source

Source: https://habr.com/ru/post/922896/


All Articles