ExpressJS 4 在Nginx代理下的自定义Header问题
发布于 10 年前 作者 szanlin 4764 次浏览 最后一次编辑是 8 年前 来自 问答

环境: Express 4 ,Nginx 通过req.headers 获取不到自定义的Header,也获取不到 x-forwarder-* 信息

> var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
>
2 回复
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_set_header X-NginX-Proxy true;
        proxy_pass http://127.0.0.1:3000;
        proxy_redirect off;
}
function getClientIp(req){
        return req.headers['x-forwarded-for'] ||
        req.connection.remoteAddress ||
        req.socket.remoteAddress;
        //req.connection.socket.remoteAddress;
}
回到顶部