socket获取ip
if(socket.handshake.headers['x-forwarded-for'] != null){
ip = socket.handshake.headers['x-forwarded-for'];
}else{
ip = socket.handshake.address;
}
express获取ip
let pid = request.headers['x-forwarded-for'] || request.connection.remoteAddress;
当获取不到IP的时候,是nginx反向代理设置有问题
正确是设置
socket
proxy_pass http://127.0.0.1:8089;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
http
proxy_pass http://127.0.0.1:8088;
proxy_redirect off;
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;