简体   繁体   中英

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

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;

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'];
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM