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

I have in the *.conf file for my domain the following:

server { 
  listen 80; 
  server_name domain.tld; 
  access_log /var/log/nginx/domain.tld.accesslog; 
  root /var/www/domain.tld; 
  index index.htm index.html;
}

My question is... what if I wanted to make all subdomains use the same document root (and same settings in general) as domain.tld?

I tried this:

server { 
  listen 80; 
  server_name domain.tld; 
  server_name *.domain.tld; 
  access_log /var/log/nginx/domain.tld.accesslog; 
  root /var/www/domain.tld; 
  index index.htm index.html;
}

But that didn't work, even after reloading. Any ideas?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

There's special syntax "sugar" to handle this gracefully:

server { 
    listen 80; 
    server_name .domain.tld; # ← this one's sweety
    access_log /var/log/nginx/domain.tld.accesslog; 
    root /var/www/domain.tld; 
    index index.htm index.html;
}

but in case you're ex-assembler programmer counting CPU-cycles in your spare time, you might want to trade simplicity of shorter notation for explicit long listing of server's names (10x2 @cnst).

share|improve this answer
1  
wrong, server_name .domain.tld; is actually the worst way to do this: nginx.org/en/docs/http/server_names.html –  cnst Mar 28 '13 at 18:19
1  
@cnst, lemme correct you: nginx.org/en/docs/http/server_names.html#optimization ) –  poige Mar 28 '13 at 18:28
2  
I think the whole micro-optimization point here is fairly irrelevant, but how does the link you give correct him at all? It seems to agree with him unless I missed something: Searching wildcard names hash table is slower than searching exact names hash table because names are searched by domain parts. Note that the special wildcard form “.example.org” is stored in a wildcard names hash table and not in an exact names hash table. –  gparent Mar 28 '13 at 18:42
1  
@gparent, LOL. It corrects him in a way of supplying link exactly to proper section — mine points to Optimization (which can be considered as another micro-optimization). –  poige Mar 29 '13 at 1:56
1  
Oh, I see. I saw it after your edit so it was the same link for me here. –  gparent Mar 29 '13 at 3:51
add comment
  server_name domain.tld; 
  server_name *.domain.tld;

I think the above is the problem. Try this instead:

  server_name domain.tld *.domain.tld;
share|improve this answer
2  
@ceejayoz, did you read the documentation? seems, that not at all.. quote from nginx.org/en/docs/http/ngx_http_core_module.html#server_name server { server_name example.com *.example.com www.example.*; } The first two of the above mentioned names can be combined: server { server_name .example.com; } –  cadmi Mar 28 '13 at 17:20
2  
@ceejayoz why did you have to remove line blank? there was no problem with the answer, and it was not misformatted in any way, and removing a line blank like that would seem like an unjustified edit –  cnst Mar 28 '13 at 17:55
2  
In my opinion, it's significantly clearer what's happening without the line break - that it's all part of one config directive. –  ceejayoz Mar 28 '13 at 17:59
2  
@poige, are you kidding me? did you actually read the docs? you're basically correct when 100% reversed. See nginx.org/en/docs/http/server_names.html. Based on the data in the question, there's no way to know what the most optimal solution should actually be. –  cnst Mar 28 '13 at 18:21
2  
@poige, well, what you're doing is imposing your own "cool" optimisation, which might be faster to write, and easier to modify, but it's less intuitive to use, might consumes [slightly] more resources (however insignificant), and is less of a direct response to the actual question; as such, sorry, but I objectively fail to see why would you categorise this answer as a rather suboptimal one, when it's clearly not –  cnst Mar 28 '13 at 18:48
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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