Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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   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 '14 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 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 '14 at 13:27
up vote 1 down vote accepted

Read how nginx chooses server block

In you case first server listens to *:80 and second to, and when you go to http://localhost/ you browser connects to 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 '14 at 15:53

I have strange feeling that a Bonjour service has something with that. Bonjour service do multicast dns for .local domains

share|improve this answer

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