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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With