PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, June 25, 2022

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

 June 25, 2022     php, reverse-proxy     No comments   

Issue

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?


Solution

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.



Answered By - Marc
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing