w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Calling localhost on a multidomain nginx server

The solution is to add a line to the file : /etc/hosts api.domain.com

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 ( 56(84) bytes of data.
64 bytes from localhost ( icmp_req=1 ttl=64 time=0.026 ms
64 bytes from localhost ( icmp_req=2 ttl=64 time=0.030 ms
64 bytes from localhost ( icmp_req=3 ttl=64 time=0.031 ms
64 bytes from localhost ( 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 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

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.