Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get client ip address in request hapijs for node

I need to get the ip address of client using my application in the request of hapijs node... we are using pm2 and Nginx for server to run live and we have used the request.info.address but it gives the server local ip as 127.0.0.1 but what i need was the client's IP who uses my application eg: 192.x.x.111 ...

like image 854
sunilsmith Avatar asked Apr 24 '26 04:04

sunilsmith


2 Answers

In case if you are running the server behind a Nginx proxy server you would have to use

    req.headers['x-real-ip']

else you can use

req.info.remoteAddress
like image 52
Partinder Singh Avatar answered Apr 25 '26 17:04

Partinder Singh


You should check the configuration on the reverse proxy (nginx) and see if you are sending the ip and how you're doing it.

For example (nginx conf):

server {
    listen       0.0.0.0:80;
    server_name  [your_domain];
    root /webroot/[your_domain or webapp name];
    access_log  /var/log/nginx/[your_domain or webapp name].log;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://127.0.0.1:[port assigned to node];
        proxy_redirect off;
    }
}

In this case, you will get the client's IP via the header X-Real-IP.

So in HapiJS, where you have access to the request object:

const ip = request.headers['x-real-ip'] || request.info.remoteAddress;
like image 45
luismatute Avatar answered Apr 25 '26 19:04

luismatute