我遇到了一个关于php curl的奇怪问题 .
它将连接到DNS服务器以解析,即使主机位于/ etc / hosts中!
软件版本:
Server :
CentOS release 6.3 (Final)
Linux Curl version :
curl --version
curl 7.47.1 (x86_64-pc-linux-gnu) libcurl/7.47.1 OpenSSL/1.0.1e zlib/1.2.3 libidn/1.18
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: IDN IPv6 Largefile NTLM NTLM_WB SSL libz UnixSockets`
PHP version :
php -v
PHP 5.4.41 (cli) (built: Aug 11 2015 22:29:40)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
PHP curl version :
php -r "var_export(curl_version());"
array (
'version_number' => 471040,
'age' => 3,
'features' => 952221,
'ssl_version_number' => 0,
'version' => '7.48.0',
'host' => 'x86_64-redhat-linux-gnu',
'ssl_version' => 'OpenSSL/1.0.1e',
'libz_version' => '1.2.3',
服务器配置:
/etc/hosts :
127.0.0.1 lalala.examplekkk.com
/etc/resolv.conf :
nameserver 8.8.8.8
/etc/nsswitch.conf
hosts: files dns
命令执行结果:
PING lalala.examplekkk.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.041 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.035 ms
....
strace php -r '$curl=curl_init();$url="http://lalala.examplekkk.com";curl_setopt($curl,CURLOPT_URL,$url);$content=curl_exec($curl);echo $content;'
...
open("/etc/hosts", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=1308, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f85c7b5c000
read(3, "127.0.0.1 localhost localhost."..., 4096) = 1308
...
fcntl(3, F_SETFD, FD_CLOEXEC) = 0
connect(3, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("8.8.8.8")}, 16) = 0
sendto(3, "\177{\1\0\0\1\0\0\0\0\0\0\6lalala\nexamplekkk\3c"..., 39, MSG_NOSIGNAL, NULL, 0) = 39
poll([{fd=3, events=POLLIN|POLLRDNORM}], 1, 0) = 0 (Timeout)
...
我很困惑,为什么php curl需要连接到dns服务器来解析主机名?
我执行命令'strace curl'http://lalala.examplekkk.com',没有连接到8.8.8.8就可以了 .
它是php-curl中的一个错误吗?