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?
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
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
Mojolicious::Plugin::ClientIP
... Did that not work out?Mojolicious::Plugin::RemoteAddr
be a solution for your problem?