cURL无法连接到localhost但浏览器可以

 xiejiahui62272 发布于 2023-02-05 12:34

我在本地运行Rails应用程序(瘦服务器),我可以从浏览器本地连接(localhost:3000),但是当我尝试使用curl时,我得到:

curl -H 'id:1' -i 'http://localhost:3000/api/data' -v

* Hostname was NOT found in DNS cache
*   Trying ::1...
* Adding handle: conn: 0x7fd121808200
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7fd121808200) send_pipe: 1, recv_pipe: 0
* Connection failed
* connect to ::1 port 3000 failed: Connection refused
*   Trying fe80::1...
* Connection failed
* connect to fe80::1 port 3000 failed: Connection refused
* Failed to connect to localhost port 3000: Connection refused
* Closing connection 0
curl: (7) Failed to connect to localhost port 3000: Connection refused

这曾经工作得很好,但我最近更新到小牛队,我怀疑可能已经破坏了一些东西.我也可以从网上成功卷曲.

2 个回答
  • 这是一个卷曲错误(一个奇怪的错误),如果/ etc/hosts中的IPv6条目没有响应,则curl无法回退到IPv4.

    您可以通过该-4选项强制它使用IPv4 .

    2023-02-05 12:35 回答
  • 代替

    curl localhost:3000
    

    尝试

    curl 0.0.0.0:3000
    

    有用.
    如果没有,请在启动rails服务器时检查输出:

    => Booting WEBrick
    => Rails 3.2.13 application starting in development on http://0.0.0.0:3000
    => Call with -d to detach
    => Ctrl-C to shutdown server
    [2014-10-28 15:30:08] INFO  WEBrick 1.3.1
    [2014-10-28 15:30:08] INFO  ruby 2.0.0 (2014-02-24) [x86_64-darwin12.5.0]
    [2014-10-28 15:30:08] INFO  WEBrick::HTTPServer#start: pid=4004 port=3000
    

    使用第2行末尾的url而不是 ENV['HOST']

    2023-02-05 12:36 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有