Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get IP address in Mojolicious?

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?

like image 564
andrei Avatar asked Aug 31 '25 22:08

andrei


1 Answers

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

like image 126
chason Avatar answered Sep 03 '25 19:09

chason