Curl request wont resolve to localhost

51,317

Solution 1

Your comment: "it listens on 192.168.2.1 and then a public IP. loopback device is 127.0.0.1". So, Apache isn't listening on 127.0.0.1? If that's the case, that is your problem. Configure Apache to listen to all interfaces, or at least add the loopback interface to what Apache is listening on.

Next issue: "If I make a curl request to sub.domain.com on that machine it will sit there in an infinite loop until it times out after a few minutes." What do you get when you ping sub.domain.com? This issue feels like a name resolution problem on that machine, as you're able to connect successfully from another machine.

Solution 2

You likely need to specify the host headers using curl or else Apache doesn't know what page to display - depending on how you are set up localhost will probably show an apache default page, while your virtual host (sub.domain.com) will show the page you expect.

Try using:

curl -H "Host:sub.domain.com" 127.0.0.1

Solution 3

Do ping sub.domain.com from another machine and record ip address in responses (xxx.yyy.zzz.www). Then from apache machine use:

curl -H "Host:sub.domain.com" xxx.yyy.zzz.www
Share:
51,317

Related videos on Youtube

Ashley Smith
Author by

Ashley Smith

Updated on September 18, 2022

Comments

  • Ashley Smith
    Ashley Smith over 1 year

    I have an ubuntu server running apache on localhost / port 80. It's domain is sub.domain.com

    If I make a curl request to http://localhost on that machine it will error with 'couldnt connect to host' immediately.

    If I make a curl request to sub.domain.com on that machine it will sit there in an infinite loop until it times out after a few minutes.

    If I open sub.domain.com in my web browser on another machine it shows my html page.

    If I make a curl request to sub.domain.com with another machine it will connect.

    What's the deal here?

    • Admin
      Admin about 12 years
      What does your /etc/hosts file look like on the server? Can you add it to the question. And check that /etc/nsswitch.conf has hosts: files dns somewhere (unless you know that it should be different).
    • Admin
      Admin about 12 years
      What does netstat -anp|grep :80|grep Listen say? Does your apache only listen on specific IPs, but not on 0.0.0.0 or 127.0.0.1 specifically?
    • Admin
      Admin about 12 years
      it listens on 192.168.2.1 and then a public IP. loopback device is 127.0.0.1
  • Ashley Smith
    Ashley Smith about 12 years
    That gives me a curl error: couldn't connect to host.
  • Ashley Smith
    Ashley Smith about 12 years
    Gives me 192.168.2.1. The loopback device is 127.0.0.1. Still timed out.
  • Dusan Bajic
    Dusan Bajic about 12 years
    Odd. I would check firewall on apache machine next.