1

I have a local web site called http://localhost.example.com but now when I try to open it in a web browser it redirects me to http://www.example.com. This used to work before. What I changed in the system was the configuration of AWS CLI and the Bitbucket local configuration in order to use two different accounts. Nothing more. I believe that changes are irrelevant. Aren't they?

All the information and files I provide are from local workstation. I'm using OS X 10.12 (beta 2).

The relevant part of /etc/hosts is this:

127.0.0.1   localhost
255.255.255.255 broadcasthost
::1 localhost 

127.0.0.1   localhost.example.com
127.0.0.1   babydiapi.example.com
127.0.0.1   localhost.mi.example.com
127.0.0.1   localhost.qr.example.com
127.0.0.1   localhost.pay.example.com
127.0.0.1   playground.com
127.0.0.1   payments.com
127.0.0.1   localhost.comunidad.example.com
198.58.110.224  web1.example.com
198.58.115.115  web2.example.com
45.56.67.40 web3.example.com

But I make ping localhost.example.com I get this:

PING localhost.example.com (104.200.16.137): 56 data bytes
64 bytes from 104.200.16.137: icmp_seq=0 ttl=49 time=166.894 ms

I tried to do ping http://localhost.example.com but it didn't worked:

ping: cannot resolve http://localhost.example.com: Unknown host

I suppose this is a normal behavior.

Which is the staging server for my application. Clearly not localhost.

I read somewhere that I should use host localhost.example.com, but I still get this:

localhost.example.com has address 104.200.16.137

I use Nginx as my local web server, the configuration looks like this:

server {
    listen   127.0.0.1:80;
    listen   127.0.0.1:443 ssl;
    server_name localhost.example.com;
    server_name_in_redirect off;
    error_page 404 /www/error/404.html;
    error_page 503 /www/error/404.html;

    ssl_certificate /usr/local/etc/nginx/ssl/localhost.crt;
    ssl_certificate_key /usr/local/etc/nginx/ssl/localhost.key;


    access_log /usr/local/etc/nginx/logs/www.example.com.access.log;
    error_log /usr/local/etc/nginx/logs/www.example.com.error.log;

    location ~ ^/(.*)/$ {
         rewrite ^/(.*)/$    /$1 permanent;
    }

    location ~* ^.+(\.jpg|\.jpeg|\.gif|\.png|\.js|\.ico|\.woff|\.ttf|\.eot|\.css)$ {
        root /www/www.example.com/webroot;
        access_log        off;
        expires          365d;
    }
    location / {
        root   /var/www/www.example.com/webroot/;
        index  index.php index.html index.htm;
        if (-f $request_filename) {
            break;
        }
        if (-d $request_filename) {
            break;
        }

        if (-f $request_filename) {
            break;
        }


        # Trigger 503 response on maintenance
        set $mantenimiento 0;
        if (-f /var/www/www.example.com/.mantenimiento) {
            set $mantenimiento 1;
        }
        if ($remote_addr = 127.0.0.1) {
            set $mantenimiento 0;
        }
        if ($mantenimiento) {
            return 503;
        }

        if (!-f $request_filename) {
            rewrite ^/(.+)$ /index.php?url=$1 last;
            break;
        }

    }
    location ~ .*\.php[345]?$ {
        set  $script     $uri;
        set  $path_info  "";

        if ($uri ~ "^(.+\.php)(/.+)") {
          set  $script     $1;
          set  $path_info  $2;
        }
        include fastcgi_params;
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_index   index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/www.example.com$fastcgi_script_name;
        fastcgi_param  SCRIPT_NAME      $script;
        fastcgi_param  PATH_INFO        $path_info;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_read_timeout 300;
    }

    location @error_web {
        root /var/www/error;
        index 404.html;
    }
 }

Do you have any ideas or hints? I have been looked over the internet for about a week and found nothing helpful.

Jenny D
  • 27,358
  • 21
  • 74
  • 110
Vladimir Nul
  • 63
  • 1
  • 9
  • Is that host file on your server or your workstation? Are you doing the testing from the server or your workstation? What OS are you using? Suggest you clarify the question. – Tim Jul 12 '16 at 20:50
  • Thanks, I have just updated the question. It's my local workstation I'm testing from. I use OS X 10.12 beta 2. – Vladimir Nul Jul 12 '16 at 20:50
  • You're using OS-X for your workstation and server? You've said all configuration files are for your location workstation, but you have a ping time of 160ms, which suggests Nginx is running on a server which probably has a different OS. You should also include relevant logs. – Tim Jul 12 '16 at 20:59
  • No, Nginx is running on my local machine, but when I ping it it shows the IP of the staging server (which is located in Linode running Debian) and not 127.0.0.1. Which logs should be relevant? – Vladimir Nul Jul 12 '16 at 21:03
  • 1
    Could you please execute: cat /etc/nsswitch.conf and send us the result? – Yarik Dot Jul 16 '16 at 09:12
  • `cat: /etc/nsswitch.conf: No such file or directory` I believe this file doesn't exist in OSX/macOS – Vladimir Nul Jul 16 '16 at 23:01

2 Answers2

2

I'm curious as to whether your /etc/hosts is no-longer the same as /private/etc/hosts. From my 10.11.5 machine, I see that they should be a hard link (same inode number as seen in the first field in the ls -li output below:

Camerons-MacBook-Air:~ cameron$ ls -li /private/etc/hosts /etc/hosts
12484749 -rw-r--r--  1 root  wheel  247 15 Jul 21:01 /etc/hosts
12484749 -rw-r--r--  1 root  wheel  247 15 Jul 21:01 /private/etc/hosts

Is this still the case for you? Do /etc/hosts and /private/etc/hosts refer to the same file on disk?

On a Linux host, I would have also wondered about /etc/nsswitch.conf, but I see OS X doesn't seem to have that; I'm not sure what it would use instead though. This file controls the lookup-method order for resolving names such as user-names or host-names.

Cheers, Cameron

Cameron Kerr
  • 3,919
  • 18
  • 24
  • Thanks, this was a starting point for finding the problem. However, the solution was provided by the other answer. I appreciate your help. – Vladimir Nul Jul 16 '16 at 22:34
1

I'd love to test this for you but I don't have OSX.

There is some discussion out there about this problem. People are saying formatting issues (missing/lack of/incorrect hidden characters) in the hosts file can lead to it being ignored. See the following question and scroll down to the answers and comments.

https://apple.stackexchange.com/questions/158117/os-x-10-10-1-etc-hosts-private-etc-hosts-file-is-being-ignored-and-not-resol

You can look into this craziness also. Again to do with formatting of the hosts file.

https://github.com/devopsgroup-io/vagrant-hostmanager/issues/60

What editor are you using? Can you rewrite the whole file carefully using vi, nano or some other Unix editor?

Don't use host or nslookup, dig, etc... they will bypass the hosts file and query DNS servers. Keep on trying with ping localhost.domain.com until you get 127.0.0.1.

Ryan Babchishin
  • 6,160
  • 2
  • 16
  • 36
  • Thanks, I cannot believe I didn't try this out first. The las time I edited the file I was using Sublime , and in the bash history that was on the top last commands so I always used this. I tried now with vim and there it's clearly about line endings, so I want back to Sublime and changed the line ending configuration to Unix. Thanks a lot. – Vladimir Nul Jul 16 '16 at 16:05
  • @VladimirNul You're welcome. Can I have the bounty? – Ryan Babchishin Jul 16 '16 at 18:57
  • Of course, it says I have to wait 14 hours, but I will as soon as I can. – Vladimir Nul Jul 16 '16 at 20:28
  • It should be OK now – Vladimir Nul Jul 17 '16 at 17:04
  • sometimes its just the local DNS cache and could be cleared by `dscacheutil -flushcache && sudo killall -HUP mDNSResponder` – Unicornist Oct 30 '18 at 14:23