narcuri1
By:
narcuri1

www pointed to wrong server block. Ubuntu/Nginx

November 24, 2015 1.1k views
Node.js Nginx Ubuntu

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.

4 comments
  • The server_name directive in the server blocks you provided is what controls this behavior. Fix your second site so that it includes the domain you want it to respond to.

  • I have:

    server_name site2.com www.site2.com;
    

    for the second site. Is that not right?
    *edit: I just realized I typed server_name site1.com www.site1.comin my original post. I meant site2 so I think what I have is correct.

  • Honestly, if you insist on redacting the domain names, there's nothing I can do to help. This is really a matter of typing it correctly on the site involved and making sure DNS resolves to that web server. Make sure you reload the web server after making the changes and that the files are being read correctly by nginx.

  • Honestly, if you insist on redacting the domain names, there's nothing I can do to help. 
    

    I'm not insisting anything. I felt it was easier to write and I can't edit my original post to change it. You really can't do anything unless you know the exact domain name? The server_name's reflect the domains that they should resolve to. In any case I found a solution and have answered my own question.

2 Answers

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

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

Have another answer? Share your knowledge.