我使用Haproxy在同一台机器上运行websocket服务器和Lighttpd web服务器。 我想从运行在Lighttpd服务器上的Web应用程序中检索机器的外部IP地址。
没有haproxy这个工程:
<?php $myip = $_SERVER['SERVER_ADDR']; $myurl = 'http://'.$myip.'/pathToProject/'; define('URL', $myurl); ?>
但是在代理$ _SERVER ['SERVER_ADDR']后面; 返回127.0.0.1,它不适用于我正在使用的PHP框架。
haproxy.conf
global maxconn 4096 nbproc 1 defaults mode http frontend all 0.0.0.0:80 timeout client 86400000 default_backend www_backend acl is_websocket hdr(Upgrade) -i WebSocket acl is_websocket hdr_beg(Host) -i ws use_backend socket_backend if is_websocket backend www_backend balance roundrobin option forwardfor timeout server 30000 timeout connect 4000 server apiserver 127.0.0.1:8080 weight 1 maxconn 1024 check backend socket_backend balance roundrobin option forwardfor timeout queue 5000 timeout server 86400000 timeout connect 86400000 server apiserver 127.0.0.1:8082 weight 1 maxconn 1024 check
您将需要在您的HAproxyconfiguration文件中添加选项"option forwardfor" ,以便HAproxy将访客真实IP地址添加一个新的标题。
之后,在你的PHP代码中,检查HTTP_X_FORWARDED_FOR头,而不是REMOTE_ADDR
foreach (getallheaders() as $name => $value) { echo "$name: $value<br>\n"; }
详细的答案可以在这里find: haproxy和转发客户端IP地址到服务器
从理论上讲,这正是你可以解决的问题
option originalto
在你的前端,根据文档 。
您的应用程序将不得不依靠X-Original-To标题。
对于Apache,你需要类似mod rpaf的东西。 我不知道这是Lighttpd,但也许这里的链接可以帮助你; http://lowendtalk.com/discussion/2259/real-ip-module-for-lighttpd
这是我解决它(只适用于Linux):
$myip = shell_exec("ifconfig eth0 | awk '/inet / {print$2}' | cut -d: -f2"); $myurl = 'http://'.$myip.'/page/'; define('URL', $myurl);