能够通过ping连接但无法通过curl连接

11

我正在测试从本地机器连接到远程服务器。 使用ping命令可以成功连接,但是如果我尝试使用curl,它就会挂起:

ping正常工作:

$ ping db-machine-02
Pinging db-machine-02.comp.org [xxx.xx.x.xxx] with 32 bytes of data:
Reply from xxx.xx.x.xxx: bytes=32 time=12ms TTL=51
Reply from xxx.xx.x.xxx: bytes=32 time=12ms TTL=51
Reply from xxx.xx.x.xxx: bytes=32 time=12ms TTL=51
Reply from xxx.xx.x.xxx: bytes=32 time=12ms TTL=51

Ping statistics for xxx.xx.x.xxx:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 12ms, Maximum = 12ms, Average = 12ms

使用curl时失败/超时:

$ curl -v db-machine-02
* STATE: INIT => CONNECT handle 0x600077108; line 1332 (connection #-5000)
* Added connection 0. The cache now contains 1 members
* STATE: CONNECT => WAITRESOLVE handle 0x600077108; line 1373 (connection #0)
*   Trying xxx.xx.x.xxx:80...
* TCP_NODELAY set
* STATE: WAITRESOLVE => WAITCONNECT handle 0x600077108; line 1452 (connection #0)
* Connection timed out after 300181 milliseconds
* multi_done
* Closing connection 0
* The cache now contains 0 members
curl: (28) Connection timed out after 300181 milliseconds

为什么我可以使用 ping 命令连接,而无法使用 curl 命令连接?

2个回答

12

这可能是由于远程机器的80端口被关闭所致。 检查您的防火墙规则,确保此端口已开放。

Ping不连接端口(它使用ICMP),因此ping可以在未打开端口的情况下正常工作。

另外提醒一下,您的ping和curl命令显示您正在尝试连接到“db-machine-02”。请注意,默认情况下,数据库不通常通过80端口进行通信。


那么,如果不指定端口,使用curl无法测试连接是不可能的吗? - u123
没错。如果您没有指定端口或协议,curl 将默认尝试建立到端口 80 的 HTTP 连接。正如您在 curl 命令的输出中所看到的那样。 - E13

2
如果使用代理服务器,如果代理服务器无法“看到”目标主机,则可能会遇到问题。例如,如果使用测试或开发环境,并且您的Linux开发主机配置为使用公司代理,则可能需要配置或覆盖no_proxy环境变量。
例如:覆盖no_proxy使curl能够在从本地VM开发框访问本地VM Web服务器时正常工作(并且http_proxy设置为公司代理)。在Linux中,一种选择是在发出curl命令时覆盖no_proxy
joe@vm-dev-host:~$ no_proxy="192.168.1.123" curl -X GET "http://192.168.1.123/somedata"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接