curl返回http_code为000的一种情况

我在服务器监控程序ServerDog的Curl模块中,使用curl命令来实现对一些网络链接的监控。如:

curl -o /dev/null --connect-timeout 10 --max-time 50 -X GET -s -w http_code=%{http_code},ssl_verify_result=%{ssl_verify_result},time_namelooe_connect},time_appconnect=%{time_appconnect},time_redirect=%{time_redirect},num_redirects=%{num_redirects},time_pretransfer=%{time_pretransfer},time_starttransfer=%{time_starttransfer},time_total=%{time_total},size_download=%{size_download},speed_download=%{speed_download} https://src.xxx.com/dancen/curl.jpg

 

在开发阶段,一切正常,当ServerDog部署到CentOS 7服务器以后,一堆报警就来了,curl无法访问部分服务器,ServerDog认为服务器出了故障,但实际上服务器是正常运行的。

 

为了确认问题,直接在ServerDog所在服务器执行上面的curl命令,其返回结果为:

http_code=000,ssl_verify_result=0,time_namelookup=0.000,time_connect=0.021,time_appconnect=0.281,time_redirect=0.281,num_redirects=0,time_pretransfer=0.000,time_starttransfer=0.000,time_total=0.281,size_download=0,speed_download=0.000

 

http_code为000,不是我们一般所知道的200、3xx、4xx、5xx之类的状态码,而且命令是马上执行完毕的,也就是说命令马上就失败了。

 

尝试之

1. 直接在浏览器访问域名https://src.xxx.com/dancen/curl.jpg,一切正常。

 

2. 在ServerDog的开发环境,一台Windows上执行以上同样的命令,返回结果也是正常:

http_code=200,ssl_verify_result=0,time_namelookup=0.028,time_connect=0.050,time_appconnect=0.202,time_redirect=0.000,num_redirects=0,time_pretransfer=0.202,time_starttransfer=0.228,time_total=0.399,size_download=1140931,speed_download=2856841.000

 

3. 在ServerDog所在服务器,将链接由原来的CDN链接

https://src.xxx.com/dancen/curl.jpg

修改为直接访问源服务器:

https://srccdn.xxx.com/dancen/curl.jpg

这次,命令的返回结果终于正常了。

 

4. 在ServerDog所在服务器,将链接

https://src.xxx.com/dancen/curl.jpg

中,https协议修改为http协议(我们的服务器和CDN同时支持两种协议):

http://src.xxx.com/dancen/curl.jpg

这次,命令的返回结果也是正常的。

 

基于以上几点判断,难道在Linux服务器上使用curl命令访问带有https协议的cdn站点有问题!!!不太可能呀~

 

5. 继续尝试,在公司的另外一台Linux上,执行同样的命令,返回结果也是正常的。这就说明,前边的判断错误,并不是在Linux上访问基于cdn的https站点就会有问题。应该是cdn的节点上下载服务器的某些设定与ServerDog所在服务器有冲突。

 

调试之

1. 强制使用ipv4

curl -4

 

2. 强制使用特定版本的ssl

curl --tlsv1.1

 

问题依然存在:为curl命令加上-v参数,将curl命令在ServerDog部署服务器上的执行过程打印出来看一下。

curl -o /dev/null --connect-timeout 10 --max-time 50 -X GET -s -w http_code=%{http_code},ssl_verify_result=%{ssl_verify_result},time_namelookup=%{time_namelookup},time_connect=%{time_connect},time_appconnect=%{time_appconnect},time_redirect=%{time_redirect},num_redirects=%{num_redirects},time_pretransfer=%{time_pretransfer},time_starttransfer=%{time_starttransfer},time_total=%{time_total},size_download=%{size_download},speed_download=%{speed_download} https://src.guangzhuiyuan.com/dancen/curl.jpg -v

 

输出结果为:

* About to connect() to src.guangzhuiyuan.com port 443 (#0)

*   Trying 13.33.71.48...

* Connected to src.xxx.com (13.xx.71.xx) port 443 (#0)

* Initializing NSS with certpath: sql:/etc/pki/nssdb

*   CAfile: /etc/pki/tls/certs/ca-bundle.crt

  CApath: none

* NSS error -12190 (SSL_ERROR_PROTOCOL_VERSION_ALERT)

* Peer reports incompatible or unsupported protocol version.

* Error in TLS handshake, trying SSLv3...

> GET /dancen/curl.jpg HTTP/1.1

> User-Agent: curl/7.29.0

> Host: src.xxx.com

> Accept: */*

>

{ [data not shown]

* Connection died, retrying a fresh connect

* Closing connection 0

* Issue another request to this URL: 'https://src.xxx.com/dancen/curl.jpg'

* About to connect() to src.guangzhuiyuan.com port 443 (#1)

*   Trying 13.xx.xx.xx...

* Connected to src.xxx.com (13.xx.xx.xx) port 443 (#1)

* TLS disabled due to previous handshake failure

*   CAfile: /etc/pki/tls/certs/ca-bundle.crt

  CApath: none

* NSS error -12286 (SSL_ERROR_NO_CYPHER_OVERLAP)

* Cannot communicate securely with peer: no common encryption algorithm(s).

* Closing connection 1

http_code=000,ssl_verify_result=0,time_namelookup=0.000,time_connect=0.021,time_appconnect=0.287,time_redirect=0.287,num_redirects=0,time_pretransfer=0.000,time_starttransfer=0.000,time_total=0.287,size_download=0,speed_download=0.000

 

看来是SSL错误:SSL_ERROR_PROTOCOL_VERSION_ALERT。由于ServerDog所在服务器使用了错误的SSL协议版本,与cdn节点服务器不兼容,因此连接被服务器直接拒绝,没有产生实际的文件数据传输,所以正如前边看到的一样,命令很快就返回了错误结果。

 

解决办法

升级ServerDog所在服务器的nss,curl:

yum update nss nss-util nspr

yum update curl

你可能感兴趣的:(linux,curl,http_code)