首页 > 解决方案 > 可以ping通但不能curl

问题描述

我正在测试从本地机器连接到远程服务器。使用 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命令连接?

标签: curlping

解决方案


这可能是由于远程计算机上的端口 80 被关闭造成的。检查您的防火墙规则以确保此端口已打开。

Ping 不连接到端口(它使用 ICMP),这就是为什么 ping 在没有打开端口的情况下工作的原因。

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


推荐阅读