The solution is to add a line to the file :
After saving the file if you run a ping on your server it should return
something like :
user@domain:~$ ping api.domain.com
PING api.domain.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_req=1 ttl=64 time=0.026 ms
64 bytes from localhost (127.0.0.1): icmp_req=2 ttl=64 time=0.030 ms
64 bytes from localhost (127.0.0.1): icmp_req=3 ttl=64 time=0.031 ms
64 bytes from localhost (127.0.0.1): icmp_req=4 ttl=64 time=0.029 ms
The file /etc/hosts will have higher priority than your machine DNS
server to resolve the name.
If everything is configured properly your cURL should now use localhost
to retrieve page from api.domain.com pages. You can verify it by running
curl with verbose option :
user@domain:~$ curl --verbose api.domain.com
* About to connect() to api.domain.com port 80 (#0)
* Trying 127.0.0.1... connected
The first lines will let you know if you are indeed using localhost ip
for the request !
In the very unlikely case it does not, check the file :
/etc/nsswitch.conf and verify the hosts line look like
this (files have priority over dns server) :
hosts: files dns