SonickSeven
SonickSeven

Reputation: 584

get ip user with nginx and node

I have a problem with nginx and node, because when i want get the ip of user with node, in my localhost works ok(no use nginx) but in my server dont work as it should. I was researching and see that the node no is the first that receive the ip, is nginx and after nginx send the request to node. then the ip that node receive is the my server and not user's ip. look the the configuration server nignx:

location / {
        proxy_pass https://fotogena.co:8000;  <-nginx send req to node
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_connect_timeout   1000;
        proxy_send_timeout      1500;
        proxy_read_timeout      2000;
}

i use "req.connection.remoteAddress" for know the ip of user and the console show me the ip of my server. somebody know how solve this problem?

thanks :D

-----------2016-04-20--------

i can solved the problem, with this line on nginx file setting

proxy_set_header X-Real-IP $remote_addr;

and node.js

req.headers['x-forwarded-for']

Upvotes: 20

Views: 20168

Answers (3)

Jonah TZ
Jonah TZ

Reputation: 21

I did:

  • Added proxy_set_header X-Real-IP $remote_addr; to nginx file
  • Added app.set('trust proxy', true)

My Express (v4.18.2) app was behind Nginx (v1.18.0 Ubuntu), and an upstream block was used.It was also a HTTPS connection.

  • req.ip return 127.0.0.1
  • req.header('x-Real-IP') return ipv4 starts with either 192, 172, or 162
  • req.headers['x-forwarded-for'] return the real ip address I can get when I verify with https://nordvpn.com/what-is-my-ip/offer-site/

Therefore, I think the suggested method works but choosing the right method to get the value is still tricky.

source code

Upvotes: 0

Piper McCorkle
Piper McCorkle

Reputation: 1074

You can configure NGINX to pass the client's IP address with the following setting:

location / {
        proxy_pass https://fotogena.co:8000;
        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-Real-IP $remote_addr;  # This line.
        proxy_connect_timeout   1000;
        proxy_send_timeout      1500;
        proxy_read_timeout      2000;
}

You can then use the HTTP header X-Real-IP from req.headers["x-real-ip"].

Upvotes: 32

ANIK ISLAM SHOJIB
ANIK ISLAM SHOJIB

Reputation: 3238

proxy_set_header X-Real-IP $remote_addr; Did not worked for me

When running an Express app behind a proxy, you have to set the application variable trust proxy to true. Express offers a few other trust proxy values which you can review in their documentation, but for now, we don’t have to mind them.

Without further ado, these are the steps to reveal a visitor’s IP address to your app:

  1. app.set('trust proxy', true) in your Express app.
  2. Add proxy_set_header X-Forwarded-For $remote_addr in the Nginx configuration for your server block.
  3. You can now read off the client’s IP address from the req.header('x-forwarded-for') or req.connection.remoteAddress;

like below in Nginx config

location /  {
            proxy_pass    http://localhost:3001;
            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 $remote_addr;  # this line
            proxy_cache_bypass $http_upgrade; 
    }

Upvotes: 9

Related Questions