In Nginx, map specific subdomains to ports, redirect all others
Just implement a default server. Here's mine
server {
listen 80 default_server;
server_name _;
return 302 https://www.example.com;
}
You can do whatever action you want.
Just to say that on AWS, I needed to add an A record "*.staging.mydomain.com" in Route53 pointing to the same EC2 instance IP (like the record for "staging.mydomain.com")