1

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?

neubert
  • 317
  • 8
  • 26

2 Answers2

2
  server_name domain.tld; 
  server_name *.domain.tld;

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

  server_name domain.tld *.domain.tld;
ceejayoz
  • 32,910
  • 7
  • 82
  • 106
cnst
  • 13,848
  • 9
  • 54
  • 76
  • 2
    @ceejayoz, did you read the documentation? seems, that not at all.. quote from http://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
  • 1
    @cadmi My only edit was to remove an unnecessary line break. Additionally, "can be" doesn't mean "has to be". – ceejayoz Mar 28 '13 at 17:23
  • 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
  • 1
    This is working solution, but rather sub-optimal one comparing to http://serverfault.com/a/493293/67675 and it's not due to an extra-white spaces or font colors. – poige Mar 28 '13 at 18:11
  • 2
    @poige, are you kidding me? did you actually read the docs? you're basically correct when 100% reversed. See http://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
  • 1
    @cnst, O_o? In case you mean "optimization" I'd have to agree with you, but what I meant in my turn was optimal way to write it down, do you see the difference? – poige Mar 28 '13 at 18:26
  • 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
  • @cnst, BTW, what d'u think bout `server_name domain.tld .domain.tld;` — it seems to be even more efficient than yours and still shorter. ;-P – poige Mar 29 '13 at 13:05
  • @poige, one symbol shorter? :-) And why would it be more efficient? I still think that `.domain.tld` is something that you have to go look at the manual for, and it doesn't seem to be the preferred way to specify `server_name`, either; so, I really don't care for the `.name` notation! – cnst Mar 29 '13 at 16:39
2

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).

cadmi
  • 7,308
  • 1
  • 17
  • 23
  • 1
    wrong, `server_name .domain.tld;` is actually the worst way to do this: http://nginx.org/en/docs/http/server_names.html – cnst Mar 28 '13 at 18:19
  • 1
    @cnst, lemme correct you: http://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 01: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 03:51