我在负载均衡器后面有许多实例。 是否可以在PHP中检测机器的名称(例如ec2-XXX-XX-XXX-XXX.compute-1.amazonaws.com)以进行debugging? 我查看了phpinfo(),它似乎没有任何一种机器的具体信息。
你将不得不使用$host = system("hostname")或类似的东西,因为PHP不会直接提供这些信息。
知识产权似乎有点牵涉其中。 取决于他们如何设置的东西,但你可以尝试
$config = system("ifconfig | grep \"inet addr\""); preg_match("/(\d{1,3})(\.\d{1,3})/", $config, $matches); $ip_addr = $matches[1];
IIRC,公共主机名实际上并不存在于服务器的任何地方。 它存储在EC2的元数据中 ,可以从任何EC2实例的169.254.169.254中获取。
$hostname = file_get_contents('http://169.254.169.254/latest/meta-data/public-hostname/');
你会得到主机名
$hostname = shell_exec("curl -s http://169.254.169.254/latest/meta-data/public-hostname");
如果你想要IP地址,然后使用
$IpAddress = shell_exec("curl -s http://169.254.169.254/latest/meta-data/public-ipv4");
这应该做到这一点:
http://php.net/manual/en/reserved.variables.server.php
$myservername = $_SERVER['SERVER_NAME'];