网站建站 优化,做网站前端和平面配合,东莞常平哪里好玩,网站开发全程实例问题
系统使用了vip与haproxy实现高可用以及对nginx进行负载均衡#xff0c;但是发现在上游的应用服务无法拿到客户端的请求ip地址#xff0c;拿到的是主haproxy机器的ip#xff0c;以下是nginx与haproxy的缩减配置#xff1a;
location ~* ^/(xx|xx) {proxy_pass http:/…问题
系统使用了vip与haproxy实现高可用以及对nginx进行负载均衡但是发现在上游的应用服务无法拿到客户端的请求ip地址拿到的是主haproxy机器的ip以下是nginx与haproxy的缩减配置
location ~* ^/(xx|xx) {proxy_pass http://upsteam;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}haproxy配置已缩减
frontend nginxmode tcpbind 0.0.0.0:443default_backend nginx_clusterbackend nginx_clustermode tcpbalance leastconnserver inst1 10.17x.xx.xx:4433haproxy配置参考地址 https://www.haproxy.com/documentation/haproxy-configuration-tutorials/load-balancing/tcp/ haproxy版本2.4.22 nginx版本1.24.0
网络架构图 原因分析
因为haproxy为了提升性能使用了tcp模式转发但是由于工作在第四层协议不会解析到第七层的http包所以无法直接将客户端ip传递给上游的Nginx。
解决方案
对haproxy与nginx采用proxy协议haproxy通过send-proxy指令将客户端ip传递给上游nginxnginx获取到客户端ip后将其写入调用上游应用的请求头中配置如下
haproxy
frontend nginxmode tcpbind 0.0.0.0:443default_backend nginx_clusterbackend nginx_clustermode tcpbalance leastconnserver inst1 10.17x.xx.xx:4433 send-proxynginx
server {listen 4433 ssl default_server proxy_protocol;server_name localhost;ssl_certificate /etc/nginx/cert/xx.pem;ssl_certificate_key /etc/nginx/cert/xx.key;charset utf-8;location ~* ^/(xx|xx) {proxy_pass http://upsteam;proxy_set_header X-Real-IP $proxy_protocol_addr;proxy_set_header X-Forwarded-For $proxy_protocol_addr;}
}参考文档地址 https://docs.nginx.com/nginx/admin-guide/load-balancer/using-proxy-protocol/#realip