【问题标题】:Can connect with ping but not curl可以ping通但不能curl
【发布时间】:2019-07-22 12:16:19
【问题描述】:

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

【问题讨论】:

    标签: curl ping


    【解决方案1】:

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

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

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

    【讨论】:

    • 好的,如果不指定端口就无法使用 curl 测试连接?
    • 没错。如果不指定端口或协议,curl 默认会尝试与端口 80 建立 HTTP 连接。正如您在 curl 命令的输出中看到的那样。
    【解决方案2】:

    如果使用代理服务器,如果代理服务器无法“看到”目标主机,您可能会遇到麻烦。例如,如果使用测试或开发环境并且您的 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"
    

    【讨论】:

      猜你喜欢
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多