I don't think the @Klamberext answer really answers the question. That is, the nginx web server has a default server concept. The official documentation page on the subject can be found here: How nginx processes a request. However one of the server blocks listening on some network interface/port combination will always act as the default one. If you don't specify that server block explicitly, it will be the very first server block in your configuration. That means that your server block with the server_name app.example.com www.app.example.com;
line will be the default server block for any request coming on 443 TCP port, serving any request where an HTTP Host
header won't match example.com
or www.example.com
(or if there will be no Host
header at all).
As already being said by @Klamberext, a common practice is to define a stub server block to catch all the requests where the Host
HTTP header doesn't match any of the domains you are serving. You can see an example at this SO answer. Usually such a server block contains the return 444;
statement which is a special nginx return code to immediately close the connection. However it seems that you need something opposite, and you'll need two server blocks to accomplish it, since as already being said, a single server block listening on TCP port 8080 will act as the default one no matter what is the Host
header set to:
server {
listen 8080;
server_name example.com www.example.com app.example.com www.app.example.com;
return 444;
}
server {
listen 8080 default_server;
... your config here
}
As an alternative you can check the Host
header value inside your server block, for example to block an example.com
domain and any of its subdomains:
server {
listen 8080;
if ($http_host ~ \.?example\.com$) { return 444; }
... your config here
}