大网数据广告活动
服务器运维问答

Local web app not resolving localhost

· 52CE 我爱测

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 o…

问题原文

原帖链接 · Alonso Urbano · CC BY-SA 3.0

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.

已采纳解答

原帖链接 · Ryan Babchishin · CC BY-SA 3.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-07-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。