NGINX - redirect all unconfigured subdomains

I am happy to join the community and want to share my little problem with you.

I have a wildcard entry for example.com in my DNS that points all subdomains to some kind of computer

* IN A 172.172.172.172 

While the NGINX configuration for this domain contains only the actively used subdomain names

 server { listen 10.0.0.1:80; server_name example.com www.example.com moskva.example.com www.moskva.example.com tokyo.example.com www.tokyo.example.com; ... } 

What I want to achieve directs all unconfigured subdomains, such as "error.example.com" to a specific address. Is there an elegant way to solve this problem?

Regards Arek

+4
source share
2 answers

This will instruct the site to redirect any unprecedented traffic to example.com

 server { listen 80 default_server; return 301 http://example.com; } 
+7
source

You can set the default server partition as follows:

 server { listen 10.0.0.1:80 default_server; server_name _; ... } 
0
source

Source: https://habr.com/ru/post/1480910/


All Articles