php|域名解析慢导致微信授权慢的问题

最近在做一个php小项目的时候,发现curl调用微信的授权api.weixin.qq.com,经常是需要等待很久,但是有时候却很快。
刚开始以为是网络慢问题,没去注意。后面通过打上时间日志观察发现,慢的时候基本是在5秒。既然那么有规律了,那肯定是有某个问题的。
http请求的初始过程是什么呢?dns解析。
通过ping和wget命令,发现确实在域名解析的那个过程很慢。所以,最终在host直接写死域名,解决了这个问题。
为什么是5秒呢?
【php|域名解析慢导致微信授权慢的问题】我们知道配置dns服务的地址在/etc/resolv.conf。
通过man resolv.conf可以找到
timeout:n
sets the amount of time the resolver will wait for
a response from a remote name server before retry-
ing the query via a different name server.Mea-
sured in seconds, the default is RES_TIMEOUT (see
).
查看/usr/include/resolv.h 可以找到这行代码 # define RES_TIMEOUT5/* min. seconds between retries */
问题是解决了,但是却引申出了很多知识点。
dns的解析默认超时时间是5秒
网上说,有可能是开启了ipv6却没有给这个域名分配ipv6的地址,通过查看vim /etc/modprobe.conf,发现没有开启ipv6功能。
查看域名的解析过程有dig命令


参考文章:http://www.lovelucy.info/curl-exec-wget-slow-due-to-ipv6.html
http://www.cnblogs.com/zhengyun_ustc/archive/2012/12/08/slow_dns_resolution.html

    推荐阅读