Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

REMOTE_ADDR not working

by vit (Friar)
on Jan 30, 2011 at 03:40 UTC ( #885093=perlquestion: print w/replies, xml ) Need Help??

vit has asked for the wisdom of the Perl Monks concerning the following question:

I use Perl/CGI for my web applications. To get the IP of the client who requests my website I use
my $ip_ad = $ENV{'REMOTE_ADDR'};
It works on all servers, but on my web server placed in AWS EC2 (amazon cloud) I get some same IP address for all requests. Any idea what can cause it and how to resolve it?

Replies are listed 'Best First'.
Re: REMOTE_ADDR not working
by rowdog (Curate) on Jan 30, 2011 at 04:21 UTC

    I haven't ever dealt with Amazon and I don't know much about web proxies but it sounds like you're getting a proxy address for all the requests. Maybe Amazon is running a reverse proxy?

    Well, if I'm right, there should be other headers you can use. One way to check would be to dump %ENV and see if you find any proxy headers.

      Most likely Amazon are running a load balancer, so that if you have multiple machines running the same http server, the load gets shared between them automatically. It also allows Amazon to save on public routeable IP addresses, which as we know are becoming a scarce resource.

      As others have said, you need to examine the headers, to look for one that contains the original IP address. Most likey it will be HTTP_X_FORWARDED_FOR, but it is entirely possible that Amazon have decided to do their own thing.

      The other thing to be aware of is that your clients will most likely be behind a proxy from their ISP, so you are unlikely to see their real IP address. If you are lucky, the proxy at the client end will also have put the IP address in a special header. The other possibility is that both proxies put the IP addresses in HTTP_X_FORWARDED_FOR, to create a comma separated list of IP addresses.

Re: REMOTE_ADDR not working
by dont_you (Hermit) on Jan 30, 2011 at 08:12 UTC
    This is probably caused by a proxy server, so check the content of $ENV{HTTP_X_FORWARDED_FOR}
Re: REMOTE_ADDR not working
by hsinclai (Deacon) on Jan 30, 2011 at 16:31 UTC
    vit,
    In ec2,
    $ENV{'REMOTE_ADDR'}
    works fine as intended on a single machine with a public Elastic IP assigned.. if you have instances behind ELB, then X_HTTP_FORWARDED_FOR might work but I am not 100% sure they set that header... they must or else server logs would be messed up too.

      I know that ELB is used.
      /*
      sure they set that header... they must or else server logs would be messed up too.
      */
      Could you please clarify
        vit,

        If you know your server is behind ELB, then you should be in luck, need to read some docs:) maybe start here, but I'd like to note this has nothing to do with Perl!

        ELB does pass HTTP_X_FORWARDED_FOR, which should be a start.



Re: REMOTE_ADDR not working
by Jeppe (Monk) on May 03, 2017 at 07:42 UTC
    REMOTE_ADDR breaks when there is a proxy involved. A better way is to first check for X-FORWARDED-FOR in the HTTP headers using $q->http('X-FORWARDED-FOR'). Proxies should set this field. Take note that there may be several comma-separated entries in that field. If that field is not set, get the IP from REMOTE_ADDR.
Re: REMOTE_ADDR not working
by Khen1950fx (Canon) on Jan 30, 2011 at 05:51 UTC
    From CGI Programming 101:
    #!/usr/bin/perl -T use strict; use warnings; use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use Socket; print header; print start_html("Remote Host"); my $ip = 'localhost'; my $hostname = gethostbyaddr(inet_aton($ip), AF_INET); print "Welcome, visitor from $hostname!<p>\n"; print end_html;
      How would that yield a different IP?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://885093]
Approved by McDarren
Front-paged by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2022-05-24 10:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (82 votes). Check out past polls.

    Notices?