Nginx – Ubuntu + Nginx 127.0.0.1 works but localhost doesn’t

localhostnginxUbuntu

this one is weird, because I don't have any error messages.

I have a very basic default file:

server {
    #listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default ipv6only=on; ## listen for ipv6

    root /var/www;
    index index.html index.htm index.php;

    # Make site accessible from http://localhost/
    server_name localhost;

    access_log      /var/log/nginx/default.access_log;
    error_log       /var/log/nginx/default.error_log warn;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to index.html
            try_files $uri $uri/ /index.html;
    }

    location /doc {
            root /usr/share;
            autoindex on;
            allow 127.0.0.1;
            deny all;
    }

    location /images {
            root /usr/share;
            autoindex off;
    }

Now, in shell,

ping localhost 

is fine.

However, in any browser, it displays an error. Chrome, for instance says: "Oops! Google Chrome could not find localhost".

127.0.0.1 on the other hand works in browsers.

Maybe you know where should I check for errors? Nothing in /var/log and other files are clean….

Best Answer

Firstly, ping is completely relevant to NGINX, you can ping any server that will respond to ping requests regardless of the services running.

Check;

curl -I -v http://127.0.0.1/ - Will see if the site is accessible by local address

curl -I -v http://localhost/ - Will see if the site is accessible by local hostname

curl -I -v http://serverhostname/ - Will see if the site is accessible by servers hostname

nslookup localhost - Make sure 'localhost' is resolving to 127.0.0.1

Post the output of that and if you're still having issues we can give you more guidance