Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm a newbie to nginx, but I encountered a basic problem that is totally not obvious to me.

I installed nginx using brew on my mac. That worked fine, with this basic, unchanged default config:

server {
   listen 80;
   server_name localhost;

    location / {
        root   html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

Now, I'm a developer working on multiple projects. Previously I configured a virtual host in apache for each of these, combined with a custom hosts entry like myproject1.local.

So, to test this with nginx, I edited my /etc/hosts file and added:

# /etc/hosts
127.0.0.1   mytestproject.local

Then, in the nginx config, I added the following:

server {
    listen 8080;
    listen mytestproject.local:80;
    server_name mytestproject.local;

    location / {
        root /Users/MyUserName/Projects/mytestproject/Web;
        index index.html;
    }
}

With that, I expected:

  • http://localhost should show the default website
  • http://localhost:8080 and http://mytestproject.local should show the website from mytestproject.

Instead of that, this is what happened:

  • http://localhost:8080 and http://mytestproject.local correctly shows the website from mytestproject.
  • But http://localhost also delivers files from the test project, and not the ones from the default website like I expected

So, this is my question:

Where was I wrong? What do I need to configure to have localhost show the default website while anything else on other ports and with other custom domains (via /etc/hosts) show their corresponding content, and not mixing that up?

Thanks in advance.

share|improve this question

closed as off-topic by Iain, mdpc, MadHatter, Jacob, Jim B May 8 at 3:46

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions must be relevant to professional system administration. Server Fault is dedicated to professional system and network administrators. End user and enthusiast questions are off-topic (contact your system administrator or hire a professional to help you out). Please see the Help Center for more information." – mdpc, MadHatter, Jacob, Jim B
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Perhaps this question would be better off moved to StackOverflow.com? As a customer support professional myself I like having a local environment on which to test, but am new to setting this up on a Mac. This seems like a legitimate question for professional developers, although not necessarily professional admins. –  Doktor J Jun 26 at 13:27

2 Answers 2

up vote 1 down vote accepted

Read how nginx chooses server block http://nginx.org/en/docs/http/request_processing.html#mixed_name_ip_based_servers.

In you case first server listens to *:80 and second to 127.0.0.1:80, and when you go to http://localhost/ you browser connects to 127.0.0.1:80 and nginx choose second server block because it's better match to request, Host header is tested after that.

So simple solution is to replace listen mytestproject.local:80; with simple listen 80;.

share|improve this answer
    
That nailed it. Thanks. That fact is indeed a bit counter-intuitive, but when you know about it it's okay to work with that. –  Sebastian P.R. Gingter May 7 at 15:53

I have strange feeling that a Bonjour service has something with that. Bonjour service do multicast dns for .local domains https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/NetServices/Articles/domainnames.html#//apple_ref/doc/uid/20001072-202537

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.