End.
原
阿里云K8S获取客户端真实IP
阿里云容器集群,想要获取客户端的真实IP,需要手动去配置才行。
1、修改容器集群的配置文件
配置文件:kube-system/nginx-configuration
修改命令:
kubectl -n kube-system edit cm nginx-configuration
添加内容:
compute-full-forwarded-for: "true"
forwarded-for-header: "X-Real-IP"
use-forwarded-headers: "true"
保存后立即生效。
PS:forwarded-for-header 参数可以自定义
2、查看nginx-configuration配置项(也可以在这里进行图形化编辑修改)
3、Nginx配置
location / {
proxy_http_version 1.1;
proxy_set_header Connection "keep-alive";
proxy_set_header X-Real-IP $remote_addr; # 配置获取真实IP
proxy_set_header Host $host;
proxy_set_header Url $proxy_add_x_forwarded_for;
if (!-f $request_filename) {
proxy_pass http://127.0.0.1:9025;
}
}
业务程序获取请求header,x-real-ip,即可以获取到客户端真实IP了。
如:
$xri = $this->request->getHeader('x-real-ip');
$clientAddressIp = $xri[0];
End.