Nginx and php | can’t resolve hostname and make connection with FQDN

getaddrinfohostnamenginxPHPresolve

I don't know what can cause this problem, nginx configs or php or anything else but :

I can not make connection when host value for these php functions listed below is set to FQDN like http://www.domain.com or www.domain.com but problem all goes away if i put hostname ip for instance 192.2.8.1 .

 ex: @fsockopen($host, $port, $errno, $errstr, 10) 
    won't establish connection when $host = "www.somedomain.com"
    successfully connects when $host = "192.2.2.1"

listed functions suffering form the same problem so far

  • @fsockopen
  • @file_get_contents
  • phpmailer can't send email through SMTP when host is "www.somedomain.com" but succesfully send mail when host is "192.2.2.1"

all of those were working perfectly before i point my FQDN to my server and when i was testing
plus it all works both with ip and domain from my pc running wammp server and on same app, on same codes

i'm not 100% sure but i guess this problem raised after i pointed my domain and DNS to server ?!

Note : this case is related to server configs and i have provided php references to better describe the case

' downvoters can now relax, i have completely changed the description ' .

Best Answer

Ok ! Finally after a week struggling with this issue :

First of all it seems to be a very popular problem for a century !

https://bugs.php.net/bug.php?id=11058
https://www.google.com/search?hl=es&ie=UTF-8&oe=UTF-8&q=php+php_network_getaddresses%3A+getaddrinfo+failed&meta=&gws_rd=ssl

Some ppl have found individual solutions at their own but definitely it's not a PHP bug .

What this issue can cover :

  • If you can't connect with @fsockopen
  • If can't get content with @file_get_contents
  • If your php script can't send email or connect to SMTP Server
  • If you can't ping your domain or getting can't find ping: Non-existent domain
  • If you can't knock your server ip and getting getaddrinfo() failed
  • If you have problem with php_network_getaddresses directly
  • If your Wordpress throws error Unable to Connect to tcp://mydomain.com:80. Error #0: php_network_getaddresses: getaddrinfo failed: No such host is known.

--Things that can cause these problems-------------------------------------------------

[X]

First thing to check is to check your php.ini and make sure these attributes are enabled

allow_url_fopen = On
allow_url_include = On


[X]

Create a phpinfo() file [ phpinfofile.com just in case ] and check if these extensions are enabled

OpenSSL
Socket

Especially for windows user running xammp or wammp by disabling PHP_OpenSSL extension you might be unable to connect through @fsockopen



[X]

Now the important part, is to go after resolv.conf Open your resolv.conf by running

sudo nano /etc/resolv.conf
  1. Delete comments, if there is a comment line at the start completely remove it
  2. Make a change to nameservers, even if they are fine just change the order of them, this helps to purge DNS cache if there's an old cache



[X]

Strongly make sure your /etc/resolv.conf is not a symlink pointing to some directories commonly at /run/resolvconf/ or etc .. you can run

ls -la /etc/resolv.conf
and see what the result is, if it's something like

-rw-r----- 1 root root ` ` /etc/resolv.conf --------> /run/resolvconf/resolv.conf

If you have an ftp access go download your /etc/resolv.conf then rename the current one on your server to something line /etc/resolv.conf-----bk ( to have it just for backup ) and then upload the one you downloaded from your system to server /etc/

Or if you don't have access to ftp run these commands

sudo cp /etc/resolv.conf /etc/resolv.conf.bak
sudo rm /etc/resolv.conf
sudo nano /etc/resolv.conf
put your nameservers on new file 

ex:
nameserver 8.8.8.8
nameserver 8.8.4.4

and save it by pressing Ctr + X  then Type "Y" and hit enter
chmod a+r /etc/resolv.conf

In this case my issue was that !

[X]

Make sure your reslv.conf is accessible to all users

chmod a+r /etc/resolv.conf


The result should be

 -rw-r--r-- 1 root root <size> <date> 
after running ls -la /etc/resolv.conf

[X]

Next Run

sudo nano /etc/network/interfaces

Find the dns-nameservers and make sure nameservers seem fine ( preferably put whatever nameservers that are on your /etc/resolv.conf file - ex: dns-nameservers 8.8.8.8 8.8.4.4



That's all that i've discovered for this issue so far , i tried it to be understandable for anyone, sorry for annoying references to things you well know
If anybody else have something more for this matter please share it .