Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Getting visitor IP from Mojolicious::Lite behind Nginx

by silent11 (Vicar)
on Oct 18, 2016 at 19:31 UTC ( #1174238=perlquestion: print w/replies, xml ) Need Help??

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

I have to admit up front, this is probably more of an Nginx question, but here goes: I have a Mojolicious::Lite app that does some simple visitor logging. I'm capturing the IP, requested path, and timestamp of the visit to a database. This all worked fine under morbo, Mojo's built-in development web server.
my $ip = $self->tx->remote_address;
Yesterday I launched the app under hypnotoad, behind nginx and now I no longer get accurate user IPs. All requests now appear as 127.0.0.1. This would make sense as the requests are coming through nginx.

some of what I read online suggests that I can get to the real IP like this:
$self->req->headers->header('X-Real-IP')
with the associated addition to my Nginx config:
proxy_set_header X-Real-IP $remote_addr;
However I'm still getting 127.0.0.1 after modifying my app to pull X-Real-IP, and restarting the server :(

Here is my Nginx config:
upstream myapp { server 127.0.0.1:8080; } server { listen 80; server_name localhost; #include /etc/nginx/proxy_params; location / { proxy_pass http://myapp; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Real-IP $remote_addr; #proxy_set_header X-Remote-Port $remote_port; } }

Replies are listed 'Best First'.
Re: Getting visitor IP from Mojolicious::Lite behind Nginx
by beech (Parson) on Oct 18, 2016 at 23:54 UTC

    However I'm still getting 127.0.0.1 after modifying my app to pull X-Real-IP, and restarting the server :(

    :) Try Mojolicious::Plugin::RemoteAddr, it retrieves x-real-ip or x-forwarded-for or tx->remote_address

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2023-12-02 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (13 votes). Check out past polls.

    Notices?