4

I want to get the IP at each login for a user. I've tried:

my $ip_address = $controller->tx->remote_address;

but it's getting the host server IP. How should I get it?

4
  • In your other now-closed question about this you brought up Mojolicious::Plugin::ClientIP... Did that not work out?
    – Shawn
    May 12, 2020 at 8:26
  • no, that did not work out
    – andrei
    May 12, 2020 at 8:28
  • @andrei -- see if following ticket is applicable for your case
    – Polar Bear
    May 12, 2020 at 9:30
  • Could Mojolicious::Plugin::RemoteAddr be a solution for your problem? May 14, 2020 at 13:21

1 Answer 1

5

If you're behind a reverse proxy $c->req->headers->header('X-Forwarded-For') will give you what you're looking for but the proper fix is to "tell your application about it by setting the environment variable MOJO_REVERSE_PROXY"

Or if you're using Hypnotoad enable proxy support via config

# myapp.conf
{hypnotoad => {proxy => 1}};

"This allows Mojolicious to automatically pick up the X-Forwarded-For and X-Forwarded-Proto headers."

Once you do that $c->tx->remote_address will automatically give you the X-Forwarded-For value and if you wanted the original IP you'd use $c->tx->original_remote_address

source: Mojo Cookbook

2
  • Thanks, I'm using morbo to test and needed some headers anyway, so: my $headers = $c->req->headers->to_hash; my ($remote_addr,$proxy_addr) = split(/,/, $headers->{'X-Forwarded-For'}) ; $log->debug('in index remote address is ' . $remote_addr ); May 16, 2021 at 8:27
  • Please, remember there can be any number of proxies from 0 to multiple. Jul 13, 2022 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.