Skip to content

nginx proxy to nodejs ip always 127.0.0.1 #139

Closed
@bsspirit

Description

@bsspirit

I use nginx as proxy server.

nginx config

upstream nodejs__upstream {
server 127.0.0.1:3000;
keepalive 64;
}

server {
....
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://nodejs__upstream;
}
}

log4js logs: remote_addr shows 127.0.0.1

[2013-06-20 04:45:05.145] [WARN] [default] - 127.0.0.1 - - "GET / HTTP/1.0" 304 - "http://50.116.27.194:3000/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36"
GET /css/my.css 304 2ms
[2013-06-20 04:45:05.381] [WARN] [default] - 127.0.0.1 - - "GET /css/my.css HTTP/1.0" 304 - "http://moive.me/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36"

I print the remote address like this:

console.log("ip=>"+req.ip);
console.log("REMOTE_ADDR=>"+req.get['REMOTE_ADDR']);
console.log("HTTP_X_REAL_IP=>"+req.get['HTTP_X_REAL_IP']);
console.log("x-real-ip=>"+req.get['x-real-ip']);
console.log("x-forwarded-for=>"+req.get['x-forwarded-for']);

var headers = req.headers;
console.log("headers=>"+ (headers['x-real-ip'] || headers['x-forwarded-for']));
console.log("headers x-real-ip=>"+headers['x-real-ip']);
console.log("headers x-forwarded-for=>"+headers['x-forwarded-for']);

console.log("====================================================");

var proxy_ip=headers['x-real-ip'] || headers['x-forwarded-for']
if(proxy_ip) return console.log("proxy"+proxy_ip);
if(req.ip) return console.log(req.ip);

Proxy Test Result:

[2013-06-20 04:45:05.130] [INFO] console - ip=>127.0.0.1
[2013-06-20 04:45:05.130] [INFO] console - REMOTE_ADDR=>undefined
[2013-06-20 04:45:05.130] [INFO] console - HTTP_X_REAL_IP=>undefined
[2013-06-20 04:45:05.131] [INFO] console - x-real-ip=>undefined
[2013-06-20 04:45:05.131] [INFO] console - x-forwarded-for=>undefined
[2013-06-20 04:45:05.131] [INFO] console - headers=>114.252.83.33
[2013-06-20 04:45:05.131] [INFO] console - headers x-real-ip=>114.252.83.33
[2013-06-20 04:45:05.131] [INFO] console - headers x-forwarded-for=>114.252.83.33
[2013-06-20 04:45:05.131] [INFO] console - ====================================================
[2013-06-20 04:45:05.131] [INFO] console - proxy114.252.83.33
[2013-06-20 04:45:05.131] [INFO] console - req.ip127.0.0.1
[2013-06-20 04:45:05.131] [INFO] console - ====================================================

I try to update the log4js-node code in the log4js/lib/connect-logger.js line 118.

.replace(':remote-addr', req.socket && (req.socket.remoteAddress || (req.socket.socket && req.socket.socket.remoteAddress)))

to

.replace(':remote-addr',req.headers['x-real-ip'] || req.socket && (req.socket.remoteAddress || (req.socket.socket && req.socket.socket.remoteAddress)))

But it's does't wok. Can you help me ?

Thanks

Conan Zhang

Activity

bsspirit

bsspirit commented on Jun 20, 2013

@bsspirit
Author

modify log4js/lib/connect-logger.js line 118.

.replace(':remote-addr', req.headers['x-forwarded-for']||req.connection.remoteAddress)

Works.

impanda-cookie

impanda-cookie commented on May 5, 2014

@impanda-cookie

Good job! thanks!

fdansv

fdansv commented on May 27, 2015

@fdansv
Contributor

@bsspirit
I see that's suggested as a fix, but is there a particular reason why it hasn't been added to connect-logger.js? It'd be really helpful for us to have it out of the box for deploying purposes.

nomiddlename

nomiddlename commented on Jun 10, 2015

@nomiddlename
Collaborator

Connect logger now supports checking 'x-forwarded-for' in the remote addr field.

fdansv

fdansv commented on Jun 11, 2015

@fdansv
Contributor

Awesome, thanks!!!

bachvtuan

bachvtuan commented on Jul 15, 2015

@bachvtuan

Thank. I forgot about nginx stuff when I was unable to get user ip from express.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @nomiddlename@bsspirit@bachvtuan@impanda-cookie@fdansv

        Issue actions

          nginx proxy to nodejs ip always 127.0.0.1 · Issue #139 · log4js-node/log4js-node