5

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....

2
  • Does telnet localhost 80 from a shell work? How about curl http://localhost?
    – Andy Smith
    Dec 2, 2011 at 11:15
  • Another thing that can trip this up is if you have an ipv6 address. On OSX 8 localhost is routed to the ipv6 address. Compiling Nginx with --with-ipv6 and adding the the this listen directive listen [::1]:80 will work too.
    – Justin
    May 19, 2013 at 3:50

3 Answers 3

6

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

5
  • sam, thank you! Yep! curl was fine - it gave 200 OK answer for localhost and 127.0.0.1. But: root@valk-pc:~# nslookup localhost Server: 11.0.0.138 Address: 11.0.0.138#53 Non-authoritative answer: *** Can't find localhost: No answer This is the address of the router. But my /etc/hosts file contains 127.0.0.1 localhost.localdomain localhost From the other place, I read that hosts file is irrelevant though.
    – valk
    Dec 2, 2011 at 11:43
  • @valk That would be your issue, do you have an entry for localhost in /etc/hosts?
    – user80776
    Dec 2, 2011 at 11:46
  • I do have. I edited my previous reply. /etc/hosts is: 127.0.0.1 localhost.localdomain localhost 127.0.1.1 valk-pc # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ff02::3 ip6-allhosts
    – valk
    Dec 2, 2011 at 11:53
  • 2
    nslookup does not use /etc/hosts file, it rather uses the nameservers listed in the /etc/resolv.conf file. (hence people tend to add a localhost entry to their dns entry's)
    – Mark
    Dec 2, 2011 at 12:48
  • I think he means irrelevant?
    – NOCARRIER
    Oct 11, 2023 at 19:32
0

Thanks everyone! In deed nslookup isn't relevant, thanks @Mark! So I tried

sudo ping localhost 

and got a correct answer.

Finally chmod'ed /etc/hosts to 644 and now everything is working. Although, not sure whether 644 is the best thing to chmod to.

Another thing, I have no idea why initially the permissions on /etc/hosts were

-rw------- 1 root root 278 2011-12-02 22:52 /etc/hosts

but now it's kind of irrelevant.

0

I had the similar problem. Was able to connect to a server on 127.0.0.1 but not using localhost. This configuration solved it for me (especially the server_name part).

server {
    listen *:80;
    listen [::]:80;
    server_name localhost;
    ...
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .