How would I redirect subdomains to my main domain in NGINX?

Posted August 2, 2017 2.3k views
NginxUbuntu 16.04

After setting up my Ubuntu 16.04 droplet by following the documentation supplied by Digital Ocean, I noticed that my droplet will serve up content intended for my domain under any miscellaneous subdomain that is typed into the address bar.

Ideally, I would like to redirect these non-existent * addresses to and still have subdomains that I have created serve up their intended content.

Thank you ahead of time to anyone that can assist me with my question; I have not done any configuration to the default NGINX server block file beyond the initial droplet set up using the Ubuntu 16.04 set up guide on Digital Ocean.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer


In this case, what I would recommend is keeping the default Nginx server block so that all unused subdomain names are redirected to your main site, and then creating separate server blocks for each of the subdomains that you would like to use.

Here’s a step by step guide on how to setup that:


by Justin Ellingwood
When using the Nginx web server, server blocks (similar to the virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain off of a single server. In this guide, we'll discuss how to configure server blocks in Nginx on an Ubuntu 16.04 server.