Is there a way to capture all requests to arbitrary sub-domains which do not have a virtual host setup, and redirect to a custom 404 page in nginx? I will have a wild card A record setup *.example.com and all our users will have a sub-domain username.example.com. If someone enters a sub-domain which does not exist how can I redirect to a custom 404 page rather than have it resolve since wild card is setup?
Sure - though I'm not sure what you mean by "rather than have it resolve" - hopefully you don't mean preventing DNS resolution, as that's not possible from your web server.
You'll want something like this: