cURL error: name lookup timed out 这种错误怎么捕获到?linux服务器因nameservers停止工作,导致name lookup timed out的解决方法。
背景:用PHP的Curl调用对方的接口,居然返回 curl error: name lookup timed out,一堆错,显然是服务器间歇性抽风。
strace了下发现:
我发现抽风的服务器可以通过--connect-timeout 来减少给接口调用方直接提示:name lookup timed out,
是用curl发现这个问题的:
curl的超时时间设置:
使用curl时,有两个超时时间:一个是连接超时时间,另一个是数据传输的最大允许时间。
连接超时时间用 --connect-timeout 参数来指定,数据传输的最大允许时间用 -m 参数来指定。
例如:
curl --connect-timeout 10 -m 20 "http://XXXXXXX"
连接超时的话,出错提示形如:
curl: (28) connect() timed out!
数据传输的最大允许时间超时的话,出错提示形如:
curl: (28) Operation timed out after 2000 milliseconds with 0 bytes received
--connect-timeout时间短,对本机的接口(接口再访问其它域名的机器)多次访问出现几率大:
According to cURL Error Codes, error code 6 stands for Couldn't resolve host. The given remote host was not resolved.
So I guess your DNS was not propagating properly at times ?
Under the hood, both curl_exec and file_get_contents perform nearly identical operations; they both use libresolv to:
connect to a name server
issue a dns request
process the dns response
To further debug this, you can use curl_getinfo() to get detailed statistics about your requests; you can use this to get an idea of how long each part took using:
CURLINFO_NAMELOOKUP_TIME
CURLINFO_CONNECT_TIME
CURLINFO_PRETRANSFER_TIME
...
The value of
the ptr variable is "name lookup timed out".
I am using the curl_easy interface. I am *not* setting
CURLOPT_NOSIGNAL, and I am also *not* setting any timeout options
(such as CURLOPT_TIMEOUT). My guess is that this crash is caused by a
signal that is emitted. I suspect that I need to set CURLOPT_NOSIGNAL
to 1L and then configure libcurl to use c-ares.
---------------------------------------------------------------------------------------
linux服务器因nameservers停止工作,导致name lookup timed out的解决方法。
刚服务器上的所有在线代理网站突然出现name lookup timed out问题,网上搜索了一下原来是服务器的nameservers问题,打开/etc/resolv.conf,看到
search box.net
nameserver 66.90.68.15
nameserver 66.90.68.169 v. m$ V- V% d, p% d, e
8 {; n5 t: M) Z% W
ping了下66.90.68.15果然不工作了! k4 v! @: x, ]3 \6 Y, ]0 ?0 n
* K, `5 n( m/ H7 X& B+ ]- n2 a
随便在google里输入“free nameservers”找到这家9 U/ J% b. ]* O* E# i, \' [
http://www.resolvingnameserver.com/freerns.html
Our current set of name servers that you can use are:+ F6 I# b3 s, L* |! F
205.234.170.215
205.234.170.217
打开resolv.conf,替换掉原来的IP,上传。- t) x0 \! j8 y
8 K" g8 e; x) T" k6 d: e
重启下服务器,问题解决。
( s8 r: {0 P$ D) r& j; I7 Q
其实还有一家比较有名的我以前介绍过
http://www.opendns.com/
208.67.222.222
208.67.220.220
我自己的windows计算机上用过,不过如果网站打开慢的话会出现他们的广告,不是很爽,希望使用的这家不要出现这样的情况。
) E, ]+ k8 _1 X
2009年3月25日更新:9 F8 ~. O3 c6 U6 f
205.234.170.215
205.234.170.217
有点慢,有时还是会出现“name lookup timed out”的情况,所以我还是选择了5 }& D5 k% s8 F7 V
208.67.222.222
208.67.220.220
来自:http://www.9125.info/redirect.php?fid=5&tid=286&goto=nextnewset
strace了下发现:
我发现抽风的服务器可以通过--connect-timeout 来减少给接口调用方直接提示:name lookup timed out,
是用curl发现这个问题的:
curl的超时时间设置:
使用curl时,有两个超时时间:一个是连接超时时间,另一个是数据传输的最大允许时间。
连接超时时间用 --connect-timeout 参数来指定,数据传输的最大允许时间用 -m 参数来指定。
例如:
curl --connect-timeout 10 -m 20 "http://XXXXXXX"
连接超时的话,出错提示形如:
curl: (28) connect() timed out!
数据传输的最大允许时间超时的话,出错提示形如:
curl: (28) Operation timed out after 2000 milliseconds with 0 bytes received
--connect-timeout时间短,对本机的接口(接口再访问其它域名的机器)多次访问出现几率大:
According to cURL Error Codes, error code 6 stands for Couldn't resolve host. The given remote host was not resolved.
So I guess your DNS was not propagating properly at times ?
Under the hood, both curl_exec and file_get_contents perform nearly identical operations; they both use libresolv to:
connect to a name server
issue a dns request
process the dns response
To further debug this, you can use curl_getinfo() to get detailed statistics about your requests; you can use this to get an idea of how long each part took using:
CURLINFO_NAMELOOKUP_TIME
CURLINFO_CONNECT_TIME
CURLINFO_PRETRANSFER_TIME
...
The value of
the ptr variable is "name lookup timed out".
I am using the curl_easy interface. I am *not* setting
CURLOPT_NOSIGNAL, and I am also *not* setting any timeout options
(such as CURLOPT_TIMEOUT). My guess is that this crash is caused by a
signal that is emitted. I suspect that I need to set CURLOPT_NOSIGNAL
to 1L and then configure libcurl to use c-ares.
---------------------------------------------------------------------------------------
linux服务器因nameservers停止工作,导致name lookup timed out的解决方法。
刚服务器上的所有在线代理网站突然出现name lookup timed out问题,网上搜索了一下原来是服务器的nameservers问题,打开/etc/resolv.conf,看到
search box.net
nameserver 66.90.68.15
nameserver 66.90.68.169 v. m$ V- V% d, p% d, e
8 {; n5 t: M) Z% W
ping了下66.90.68.15果然不工作了! k4 v! @: x, ]3 \6 Y, ]0 ?0 n
* K, `5 n( m/ H7 X& B+ ]- n2 a
随便在google里输入“free nameservers”找到这家9 U/ J% b. ]* O* E# i, \' [
http://www.resolvingnameserver.com/freerns.html
Our current set of name servers that you can use are:+ F6 I# b3 s, L* |! F
205.234.170.215
205.234.170.217
打开resolv.conf,替换掉原来的IP,上传。- t) x0 \! j8 y
8 K" g8 e; x) T" k6 d: e
重启下服务器,问题解决。
( s8 r: {0 P$ D) r& j; I7 Q
其实还有一家比较有名的我以前介绍过
http://www.opendns.com/
208.67.222.222
208.67.220.220
我自己的windows计算机上用过,不过如果网站打开慢的话会出现他们的广告,不是很爽,希望使用的这家不要出现这样的情况。
) E, ]+ k8 _1 X
2009年3月25日更新:9 F8 ~. O3 c6 U6 f
205.234.170.215
205.234.170.217
有点慢,有时还是会出现“name lookup timed out”的情况,所以我还是选择了5 }& D5 k% s8 F7 V
208.67.222.222
208.67.220.220
来自:http://www.9125.info/redirect.php?fid=5&tid=286&goto=nextnewset
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/6472/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2013-6-21 17:27
评论列表