curl无法连接到localhost端口80

lfe*_*445 30 curl nginx

我的主机文件映射127.0.0.1localhost.

$ curl -I 'localhost'
curl: (7) Failed to connect to localhost port 80: Connection refused
Run Code Online (Sandbox Code Playgroud)

然后

$ curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.2.4
Date: Wed, 09 Apr 2014 04:20:47 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 23 Oct 2012 21:48:34 GMT
Connection: keep-alive
Accept-Ranges: bytes
Run Code Online (Sandbox Code Playgroud)

在我的主机文件中,我有

127.0.0.1   localhost
Run Code Online (Sandbox Code Playgroud)

看来curl命令无法识别输入 /etc/hosts.有人可以解释原因吗?

更新:我还没试过这个,但我发现你可以配置nginx来响应ipv4和ipv6

Joa*_*son 53

由于您::1 localhost的hosts文件中有一行,因此curl似乎正在尝试使用IPv6来联系您的本地Web服务器.

由于Web服务器未侦听IPv6,因此连接失败.

您可以尝试使用--ipv4选项curl,当两者都可用时,应强制使用IPv4连接.


six*_*bit 5

如果任何人遇到这样和接受的答案不工作(它没有为我),检查是否需要指定除80以外的端口就我而言,我是跑在轨服务器localhost:3000,是仅使用curl http://localhost,该端口的端口号为80。

curl http://localhost:3000在我的情况下,将命令更改为即可。


小智 5

就我而言,文件 ~/.curlrc 配置了错误的代理。