Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logging with morgan only shows 127.0.0.1 for :remote-addr in nodejs

i have a nodejs/express application and am logging with morgan: var morgan = require ("morgan"); var app = express(); app.use(morgan(':date[iso] :remote-addr :method :url :status :res[content-length] - :response-time ms'));

but in my logfiles (redirected from 'npm start') i find lines like this:

2014-12-21T10:02:59.365Z 127.0.0.1 GET / 304 - - 2.389 ms

showing 127.0.0.1 as remote address for all requests. i do use angular's $routeProvider after the index.html is loaded but even the / request returns 127.0.0.1.

app.get('/partials/:name', routes.partials); app.get('*', function (req, res) { res.setHeader('Content-type', 'text/html'); res.charset = 'UTF-8'; res.sendFile(__dirname + '/pub/index.html'); });

am i missing something? shouldn't the actual requester be logged here?

like image 806
kjoetools Avatar asked Sep 16 '25 09:09

kjoetools


2 Answers

Is your server behind a proxy?

Try:

app.enable("trust proxy");

(Insert line before using morgan middleware)

like image 145
pasimako Avatar answered Sep 18 '25 10:09

pasimako


Two conditions must be met.
As pasimako showed code:

app.enable("trust proxy");

and with nginx configuration (from this question):

proxy_set_header X-Forwarded-For $remote_addr;

For me works only that way.

like image 25
Aleksandr Chernyi Avatar answered Sep 18 '25 08:09

Aleksandr Chernyi