How does ghost get real IP from nginx(reverse proxy)?

172 views Asked by At

I have configured nginx as a reverse proxy. However, ghost always gets the same ip 127.0.0.1 from request forwarded to it by nginx.

How can I make ghost to get the real IP from nginx?

My configuration of nginx includes the following statements

proxy_set_header Host $http_host;    
proxy_set_header  X-real-ip $remote_addr;
1

There are 1 answers

1
Philip Tzou On BEST ANSWER

You should try retrieve the IP address first from headers:

var ip = req.headers['x-real-ip'] || req.connection.remoteAddress;

It is not recommended to override req.connection.remoteAddress directly since it will confuse other programmers you collaborated with. But it is technically possible. The remoteAddress is a getter so you can't assign value to it directly, you need to define your own getter:

req.connection.__defineGetter__('remoteAddress', function() {
    return req.headers['x-real-ip'];
});