By narcuri1
Hello,
I have two sites, each with their own domain, on one droplet. I’m running Ubuntu and nginx. The first site is a static site and the second is a NodeJS app.
www.site1.com or site1.com works fine.
When I type site2.com it points to the second site fine.
However, if I type www.site2.com it serves just the index.html of the first site with no styles/scripts.
Each are on there own server block via this tutorial. The node server block looks like this:
server {
listen 80;
server_name site1.com www.site1.com;
location / {
proxy_pass http://<DROPLET_IP>:<NODE_SERVER_PORT>;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
I have a CNAME alias for the second site that points www to site2.com.
Any help would be appreciated, thanks.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Well I found a work around via this stackoverflow post.
I added:
server {
listen 80;
server_name www.site2.com;
return 301 $scheme://site2.com$request_uri;
}
to the top of the file.
here’s the whole thing:
server {
listen 80;
server_name www.site2.com;
return 301 $scheme://site2.com$request_uri;
}
server {
listen 80;
server_name site2.com;
location / {
proxy_pass http://<ip>;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Each domain needs to be setup with the proper DNS entries. This would consist of at least one A entry and one CNAME entry, which would look like:
NOTE: domain.ext = your actual domain name and DROPLET_PUBLIC_IP = your Droplets Public IP Address.
A @ DROPLET_PUBLIC_IP
CNAME www domain.ext.
Optionally, you can add an additional A entry which is referred to as a WildCard. It’s simply an A entry that sets an asterisk (i.e. *) to point to your Droplets Public IP.
A * DROPLET_PUBLIC_IP
… so all together:
A @ DROPLET_PUBLIC_IP
A * DROPLET_PUBLIC_IP
CNAME www domain.ext.
Now, if you prefer not to use the WildCard, your server_name directive should always be set as:
server_name domain.ext www.domain.ext;
If you choose to use the WildCard (best option, IMO), then your server_name directive (for each server block) would look like:
server_name domain.ext *.domain.ext;
We’re simply replacing www with * in the server_name directive, that’s it :-).
The benefit of using the WildCard setup instead of the typical www is that when you visit a a URI that does not exist, if a WildCard is setup, it’ll route that request to the primary domain, thus, no error. If you do not use a WildCard, when someone tries to access invalid-subdomain.yourdomain.ext (i.e. a sub-domain that isn’t setup / does not exist), they’ll be met with a 404 (or whatever error is standard for your setup).
The WildCard simply helps in making sure that any requests that would normally be invalid or display and error are routed to your domain instead. This, IMO, keeps the error logs cleaner (and smaller).
NOTE: www is considered a “sub-domain”, as is anything that is placed before your domain (i.e. anything.yourdomain.ext). So if you use the WildCard, technically, you do not need the CNAME for www.
–
Once you make the above changes, DNS can take up to 24-48 hours to update, so such changes may not be immediately available. If nothing is working after 24-48 hours, then here what we need to look at:
1). Copy & Paste, verbatim, both server blocks. You can replace the domains. They really don’t matter as long as you can adjust the configuration I provide you with by replacing my configuration with your actual config :-).
2). Copy & Paste, verbatim, your DNS Zone entries for each domain name (specifically your A and CNAME entries). If you have any DNS entries that modify routing in any way, or you’re unsure, please post the entire DNS Zone (screenshots are fine).
–
I’d recommend PasteBin for #1 and Imgur for #2. This just keeps thing clean :-).
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.