This is a short post about a nginx gotcha that stumped me for a few minutes. tl;dr nginx servernames using wildcards can only occur if they are preceding a dot.
Lets suppose we have an nginx vhost like this:
This proxies to localhost:8080, which could for instance be something like tomcat or jetty. Let’s suppose we want our vhost to also proxy for requests coming in at *-apps.my.domain.com. You might try something like this:
However, this will not work, to quote the nginx docs:
A wildcard name may contain an asterisk only on the name’s start or end, and only on a dot border. The names “www.*.example.org” and “w*.example.org” are invalid.
Therefore, we need to use a regex instead, so in our example we can instead use:
Note that now we’ve converted the server_name to use a regex we need to escape the dots so they don’t revert to match any character.