I have a few websites, and when I type in a subdomain that does not exist, Nginx redirects to the first available website in /etc/nginx/sites-enabled. My config is as follows for the websites:
server {
listen 80;
listen [::]:80;
server_name *.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name *.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
root /var/www/app_name/build;
index index.html index.htm;
location / {
try_files $uri /index.html =404;
}
}