Question

www pointed to wrong server block. Ubuntu/Nginx

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.

Show comments

Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

@narcuri1

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

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;
    }
}