Saif Ali
Saif Ali

Reputation: 427

How to get real ip of visitor if our app is behind a reverse proxy?

I'm using Siteground's nginx based dynamic cache reverse proxy which serves requests and static file using it, i want to get the IP address of the visitor but im unable to get anything there's nothing even shown while print_r($_SERVER).

Here is what i tried.

$hostname = gethostbyaddr(trim($_SERVER['HTTP_X_REAL_IP']));
$hostname = gethostbyaddr(trim($_SERVER['REMOTE_ADDR']));

If someone can help?

Upvotes: 2

Views: 3074

Answers (1)

Marc
Marc

Reputation: 1430

You have to set configurations on your reverse proxy in order to forward the real IP address to your web server.

For example using Nginx, you can set headers like this :

proxy_set_header    X-Forwarded-Proto   $scheme;
proxy_set_header    X-Real-IP       $remote_addr;
proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
proxy_set_header    Host            $http_host;

Here, the X-Real-IP will provide you the real IP address on your backend server.
You must refer to the documentation of the reverse proxy software you're using (apache, nginx, etc..) for more information.

Upvotes: 2

Related Questions