DNS not working in PHP (but does in general)

2019-07-16 11:55发布

The following script:

<?php
 $dnsrecs = dns_get_record('google.com'); foreach($dnsrecs as $dnsrec){ foreach($dnsrec as $label=>$val){ echo $label.':'. $val . '<br>'; } }


  echo file_get_contents('https://google.com'); 


?>

Gives back a list of DNS records, but ends up failing on the file_get_contents, although phpinfo() says:

allow_url_fopen On  On
allow_url_include   Off

With the following error:

PHP message: PHP Warning: file_get_contents(https://google.com): failed to open stream: php_network_getaddresses: getaddrinfo failed: No address associated with hostname in /var/www/a.php on line 5" while reading response header from upstream, client: 152.66.34.10, server: xxx, request: "GET /a.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm-www.sock:", host: "xxx"

DNS resolution on the server works, with the same user as nginx is running:

# sudo -u www-data host google.com
google.com has address 216.58.212.142
google.com has IPv6 address 2a00:1450:400e:800::200e
google.com mail is handled by 50 alt4.aspmx.l.google.com.
google.com mail is handled by 10 aspmx.l.google.com.
google.com mail is handled by 30 alt2.aspmx.l.google.com.
google.com mail is handled by 40 alt3.aspmx.l.google.com.
google.com mail is handled by 20 alt1.aspmx.l.google.com.

My PHP version is recent:

# php --version
PHP 5.5.9-1ubuntu4.21 (cli) (built: Feb  9 2017 20:54:58) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

I am running it via php5-fpm, and it seems to be related to that, because sudo -u www-data php a.php works correctly.

My php.ini file is here: http://pastebin.com/qZePJdT5 And my phpinfo is here: http://pastebin.com/cAWuu8wr

Thanks.

标签: php dns fastcgi
2条回答
甜甜的少女心
2楼-- · 2019-07-16 11:58

Rebooting server (linux) and changing nothing else fixed this.

查看更多
淡お忘
3楼-- · 2019-07-16 12:22

We were seeing this exact issue with the Goolge public DNS servers. We fixed it by simply restarting PHP-FPM. This issue has been the topic of some twitter disucssions:

https://twitter.com/laravelphp/status/844181376224165890

However is does not seem to be DigitalOcean specific.

查看更多
登录 后发表回答