Question

Prolem: subdomain redirect to maindomain

Posted August 25, 2020 423 views
NginxUbuntu 20.04

Hi,
I’m creating a new subdomain (sub.example.com) on the same droplet as my main domain (example.com). I’m able to ping the subdomain and check DNS (https://www.digitalocean.com/community/tools/dns). However, whenever I come to sub.example.com, it still redirect me to my main domain’s content (the bar still shows sub.example.com).
My main domain is a wordpress website.
My sub.example.com.conf on etc/nginx/sites-available:
server {
listen 80;
root /var/www/sub.example.com;
index index.html index.htm index.nginx-debian.html;
servername sub.example.com www.sub.example.com;
location / {
try
files $uri $uri/ =404;
}
}

Hosts file: myip sub.example.com

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.

×
4 answers

To be clarify,
when i go to sub.example.com it should shows the index.html I created, but it redirect me to sub.example.com/users/login and shows the login page for user on my main domain.

Hi! That’s good you’ve verified that the DNS is resolving correctly. It seems you are on the right track with a separate nginx conf file for your sub-domain - did you restart nginx after making that file? You probably did but just in case ;)

I also like to try the new DNS / webhost config from multiple browsers and devices - the browsers today will cache the darnedest things. You could do this easily with an incognito window on your workstation and on your phone if thats available. This will verify that the problem is on the server and not just on your computer.

Otherwise, it sounds like your main domain web server (nginx) config might be overriding the subdomain config… here’s an article in the DO knowledgebase with some examples - if you have not already, please review what you have against what is presented in this tutorial:

https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

If you’re still stuck, please share a version of your main domain configuration so we can look for possible conflicts.

Hope this helps and good luck!

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...

@gnohpcntt here is your checklist:

  1. Recheck your hosts file to ensure that sub.example.com point to the actual IP of your droplet.
  2. Your Nginx configuration file looks good. You put it in the /etc/nginx/sites-available folder. So don’t forget to create a symlink to it in the /etc/nginx/sites-enabled folder too.
  3. Reload Nginx to apply changes.

You should read Step Four: Enable your Server Blocks and Restart Nginx for more details.

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...

HI @gnohpcntt,

Apart from the other good answers, can you verify the subdomain is actually a blank website or is it a WordPress as well? I’m asking as it’s possible you are using either the same database for your subdomain or having your home url set to your main domain. Check that just in case as well.

Regards,
KFSys

Submit an Answer