Every subdomain is in a separate folder. I capture the subdomain and set the root to the correct folder with this code:
server_name ~^(?<sub>.+)\.mysite\.com$;
root "/path/to/my/site/$sub";
When the sub domain does not exist, it throws a 404 error, but not using my 404 error page because the root is set to a directory that does not exist.
I can solve the problem with this code:
server_name ~^(?<sub>.+)\.mysite\.com$;
if (!-d "/path/to/my/site/$sub") {
set $sub www;
#set $sub "";
}
root "/path/to/my/site/$sub";
but obviously, it is wrong because if is evil, or have I found an exception where it is correct of me to use if
.