0

I don't know why server_name example.com matches also subdomains (at least when there's not a rule for those), if there's an specific syntax for doing so:

Nginx documentation states that you can define server_name in the following ways:

Sets names of a virtual server, for example:

server_name example.com;

Server names can include an asterisk (“*”) replacing the first or last part of a name:

server_name *.example.com;

Such names are called wildcard names.

The first two of the names mentioned above can be combined in one:

server_name .example.com;

But I tested this, and example.com behaves just as .example.com is expected, matching also all the subdomains.

This is not a problem as I can override the subdomains by setting a server for *.example.com but it seems very odd to me that if the syntax of .example.com exists with the intention to match both other two, it should mean that the other two don't match to eachother...

Why is this?

Carlos López Marí
  • 1,432
  • 3
  • 18
  • 45
  • 1
    One of the available `server` blocks for each listening port/network interface always acts as the default sever capturing all the incoming requests on that port/interface no matter of HTTP `Host` header value. The default server can be defined explicitly with the `default_server` flag of the `listen` directive otherwise it would be the first `server` block listening on that IP/port combination. Read [this](http://nginx.org/en/docs/http/request_processing.html) documentation page. – Ivan Shatsky Nov 08 '20 at 21:11
  • 1
    [Here](https://stackoverflow.com/questions/60362642/nginx-doesnt-listen-on-port-80-twice/60362700#60362700) is more detailed answer on that subject. – Ivan Shatsky Nov 08 '20 at 21:12
  • Nice, thank you. If you post just that as an answer, I will accept it. – Carlos López Marí Nov 08 '20 at 21:22
  • Further explanation: https://stackoverflow.com/a/64742545/8719655 – Carlos López Marí Nov 11 '20 at 12:31

1 Answers1

0

If your subdomains don't match any rule, they will be called back to the rule tagged as default_server, which, by default, is the first one on the file. In this case, the example.com rule.

This is the answer to the right question:

https://stackoverflow.com/a/64742545/8719655

Carlos López Marí
  • 1,432
  • 3
  • 18
  • 45