nginx反向代理之后服务端HTTP_X_FORWARDED_FOR和HTTP_X_REAL_IP获取不到客户端ip地址

进过nginx的反向代理之后,我们在服务端获取$_SERVER['HTTP_X_FORWARDED_FOR']或者$_SERVER['HTTP_X_REAL_IP']时,往往获取不到客户端的外网ip地址,此时需要在做反向代理时设置:

location / {

    proxy_pass http://www.abc.com;

    proxy_set_header Host $host; //需加上

    proxy_set_header X-Real-IP $remote_addr; //需加上

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; //需加上

}

 

按照上述方式在反向代理时设置header变量,即可在服务端拿到 HTTP_X_FORWARDED_FOR 和 HTTP_X_REAL_IP;

 

你可能感兴趣的:(nginx)