41

My host's file maps 127.0.0.1 to localhost.

$ curl -I 'localhost'
curl: (7) Failed to connect to localhost port 80: Connection refused

And then

$ 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

In my host's file I have

127.0.0.1   localhost

It appears that the curl command fails to recognize entries in /etc/hosts. Can someone explain why?

update: I've yet to try this but I've discovered you can configure nginx to respond to ipv4 and ipv6

ash
  • 1,065
  • 1
  • 5
  • 20
random-forest-cat
  • 33,652
  • 11
  • 120
  • 99

7 Answers7

62

Since you have a ::1 localhost line in your hosts file, it would seem that curl is attempting to use IPv6 to contact your local web server.

Since the web server is not listening on IPv6, the connection fails.

You could try to use the --ipv4 option to curl, which should force an IPv4 connection when both are available.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • This worked for me also while executing the bin/generate-example-metrics script to POST to imply's PIVOT interface. – ciacicode Sep 09 '16 at 14:30
  • I had the same problem and while it worked by adding the ipv4 flag, another thing which worked for me was removing ::1 from ```/etc/hosts``` – Gokul Apr 08 '21 at 04:49
16

If anyone else comes across this and the accepted answer doesn't work (it didn't for me), check to see if you need to specify a port other than 80. In my case, I was running a rails server at localhost:3000 and was just using curl http://localhost, which was hitting port 80.

Changing the command to curl http://localhost:3000 is what worked in my case.

sixty4bit
  • 7,422
  • 7
  • 33
  • 57
5

In my case, the file ~/.curlrc had a wrong proxy configured.

Bessi
  • 161
  • 1
  • 4
2

I also had problem with refused connection on port 80. I didn't use localhost.

curl --data-binary "@/textfile.txt" "http://www.myserver.com/123.php"

Problem was that I had umlauts äåö in my textfile.txt.

Toydor
  • 2,277
  • 4
  • 30
  • 48
0

I've encountered the same error before on my load balancer server. Am working with two servers and a load balancer. on my two servers I have NginX running, but on load balancer I have Haproxy. I discoved that accidentally Nginx was installed on the LB. Solution:

sudo rm /etc/nginx/nginx.conf
sudo rm -rf /etc/nginx/
sudo apt purge nginx
sudo service haproxy restart

Then run the command again curl -I localhost

0

In my case the problem was resolved when I added to nginx.conf: resolver, events, root and upstream (may be it will be useful for somebody). My nginx.conf (after I fixed the error):

worker_processes  1;
events {
  worker_connections  1024;
}    
http{
  upstream back-stream {
    server back:8080;
  }
  server {
    listen 80;
    listen [::]:80;
    server_name test.com www.test.com;
    location / {
      root   /usr/share/nginx/html;
      resolver 121.0.0.11;
      proxy_pass http://back-stream;
    }
  }
}

My docker-compose file:

version: '3.9'
services:
  nginx-proxy:
    image: nginx:stable-alpine
    container_name: nginx-proxy
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    networks:
       - network
  back:
    image: "mycustomimage"
    container_name: back
    restart: unless-stopped
    ports:
      - '81:8080'
    networks:
       - network
networks:
  network:
    driver: bridge
Andres
  • 51
  • 1
  • 6
0

Sometimes You need to make sure the web server is running. In my case, I had forgotten starting the nginx webserver after I had stopped it.

sudo service nginx status

To start it if off:

sudo service nginx start

Note: Replace nginx with apache2 if it is what you are using

curl localhost
JaSON
  • 4,843
  • 2
  • 8
  • 15