haproxy 在http头部添加后端用户真实IP

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了haproxy 在http头部添加后端用户真实IP相关的知识,希望对你有一定的参考价值。

在haproxy的配置文件中的前端bind下添加如下配置:

 http-request set-header ZXTMIP %[src]

ps:其中ZXTMIP是自定义的字段 %[src]是取得IP

测试结果:

技术分享在浏览器中打开测试域名后的输出如下:

Array ( [TEMP] => /var/tmp [TMPDIR] => /var/tmp [TMP] => /var/tmp [PATH] => /usr/local/bin:/usr/bin:/bin [HOSTNAME] => mptest.okooo.com [USER] => web [HOME] => /var/webroot [FCGI_ROLE] => RESPONDER [SCRIPT_FILENAME] => /home/oae/web/wwwroot/public/index.php [QUERY_STRING] => [REQUEST_METHOD] => GET [CONTENT_TYPE] => [CONTENT_LENGTH] => [SCRIPT_NAME] => /index.php [REQUEST_URI] => / [DOCUMENT_URI] => /index.php [DOCUMENT_ROOT] => /home/oae/web/wwwroot/public [SERVER_PROTOCOL] => HTTP/1.1 [GATEWAY_INTERFACE] => CGI/1.1 [SERVER_SOFTWARE] => nginx/1.6.2 [REMOTE_ADDR] => 192.168.1.231 [REMOTE_PORT] => 6824 [SERVER_ADDR] => 192.168.1.235 [SERVER_PORT] => 80 [SERVER_NAME] => mptest.okooo.com [REDIRECT_STATUS] => 200 [HTTP_HOST] => mptest.okooo.com [HTTP_UPGRADE_INSECURE_REQUESTS] => 1 [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/60.0.3112.78 Safari/537.36 [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 [HTTP_ACCEPT_ENCODING] => gzip, deflate [HTTP_ACCEPT_LANGUAGE] => zh-CN,zh;q=0.8 [HTTP_COOKIE] => LastUrl=; __utma=56961525.524112254.1501123742.1501123742.1501123742.1; __utmz=56961525.1501123742.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none) [HTTP_ZXTMIP] => 111.200.213.110 [HTTP_X_FORWARDED_FOR] => 111.200.213.110 [HTTP_CONNECTION] => close [PHP_SELF] => /index.php [REQUEST_TIME_FLOAT] => 1501644453.5864 [REQUEST_TIME] => 1501644453 )

本文出自 “Dr小白” 博客,请务必保留此出处http://1213503.blog.51cto.com/1203503/1952946

以上是关于haproxy 在http头部添加后端用户真实IP的主要内容,如果未能解决你的问题,请参考以下文章

haproxy+openresty实现反向代理和ip透传

HTTP X-Forwarded-For 头部字段的应用

haproxy转发真实ip

服务器 获取用户 真实ip

Haproxy的三种保持客户端会话保持方式

前端Nginx,后端Apache获取用户真实IP地址