可以ping连接但不能curl

Can connect with ping but not 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

Fails/timeout 卷曲:

$ 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 命令?

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

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

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

如果使用代理服务器,如果代理服务器无法 'see' 目标主机,您可能 运行 会遇到麻烦。例如,如果使用测试或开发环境并且您的 linux 开发主机配置为使用公司代理,那么您可能需要配置或覆盖 no_proxy 环境变量。

例如:重写 no_proxy 使 curl 在从本地 VM 开发箱访问本地 VM 网络服务器时能够工作(并且 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"