0

I have a domain example.com

I wanted to have 2 subdomains: a.example.com and b.example.com pointing to directories /var/www/a and /var/www/b respectively.

So I'm using this nginx config:

server {
  server_name a.example.com;
  root /var/www/a;
  try_files $uri /index.html;
}

server {
  server_name b.example.com;
  root /var/www/b;
  try_files $uri /index.html;
}

And it works as expected.

But when I open up c.example.com it renders /var/www/a/index.html

Note that I haven't specified any rule for c.example.com.

So why does it behave as if it were matching a.example.com?

Does nginx treats the first rule as the default one when nothing else matches? -- Even when the server_name directive in that rule doesn't really match the URL?

5
  • 1
    Yes. See docs on listen directive
    – Alexey Ten
    Nov 17, 2017 at 7:34
  • 1
    Also this worth reading nginx.org/en/docs/http/request_processing.html
    – Alexey Ten
    Nov 17, 2017 at 7:36
  • @AlexeyTen thanks! Quite a surprising behavior to be honest. But that answers my question, so if you'd like to post it as an answer I will accept it. Nov 17, 2017 at 12:10
  • As @AlexeyTen mentions it will use the first one, or you can add default_server option to the listen directive to choose a default option when the hostname does not match the list of available servers instead of relying in the order. Nov 17, 2017 at 12:11
  • It's look like that you have some *.example.com CNAME example.com record for example.com zone in the DNS that associate all undefined (and possibly unwanted) subdomains with the host where your a.example.com and b.example.com subdomains resides. That is useful behaviour but you have to handle it with separate nginx server section with default qualificator that inform user about wrong server name.
    – Kondybas
    Nov 17, 2017 at 23:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.