Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX - redirect all unconfigured subdomains

I'm happy to join community and want to share with you with my little problem.

I've got wildcard entry for example.com in my DNS which points all subdomains to some machine

* IN A 172.172.172.172

While NGINX configuration for this domain contains only 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 is directing all unconfigured subdomains like 'mistake.example.com' to specific address. Is there any elegant way of solving this problem?

Best Regards Arek

like image 414
user2385437 Avatar asked May 15 '13 10:05

user2385437


Video Answer


2 Answers

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

server {
    listen 80 default_server;
    return 301 http://example.com;
}
like image 79
Mohammad AbuShady Avatar answered Sep 28 '22 15:09

Mohammad AbuShady


You can set a default server section like this:

server {
    listen       10.0.0.1:80  default_server;
    server_name  _;
    ...
}
like image 32
Carsten Avatar answered Sep 28 '22 15:09

Carsten