When I used the php curl function today, I found it was too slow to get the result after about 5 seconds. If the same url is accessed through a browser, the page can be obtained immediately. Later, it was found that, even if php was not used, it was slow to use the native command wget in Linux to obtain webpages. This is really strange. It seems that it is not a program, but a network setting problem.
When running wget, we can see that blocking occurs in the DNS domain name resolution section.
$ Wget www.myproject.com
-- 12:17:30 -- http://www.myproject.com/
Resolving www.myproject.com... # It is stuck for about 5 seconds.
192.168.1.187
Connecting to www.myproject.com | 192.168.1.187 |: 80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: index.html?
[<=>] 5,200 --.-K/s in 0 s
12:17:35 (264 MB/s)-index.html saved [5200]
I am using a development environment where the domain name is resolved to an internal IP address. It is strange that I use ping www.myproject.com to quickly obtain the IP address and return the ICMP packet. Why is the DNS resolution of wget so slow? Www.2cto.com
Many people on Google to StackOverflow also raised this issue, saying it was a reverse DNS reverse Domain name resolution problem. Some said it was necessary to add the domain name to the host file ...... Cannot solve my problem. Finally, we suddenly thought that last week was IPv6, and our O & M team sent an email saying that the company's network was already fully enable IPv6. Is that why? According to this clue, it was clear.
$ Wget-4 www.myproject.com
Results are returned in an instant. It seems that O & M does not help us bind an IPv6 address to this domain name. After the network is upgraded, IPv6 will be resolved first by default. In the case that the domain does not have IPv6, it will wait until the IPv6 resolution fails timeout before proceeding to the previous normal process to find IPv4.
For PHP curl, you only need to add the following sentence to solve the latency problem:
Curl_setopt ($ ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
At present, many servers enable IPv6 without routing and cannot work, which leads to unexpected problems. I am deeply aware of the long IPv6 path ......