Domain.com works, *.domain.com returns Bad Request, and www.domain.com does not connect

January 16, 2018 622 views
Django Deployment DigitalOcean DNS Networking Python Frameworks Ubuntu 16.04

The site loads when I visit the droplet IP address and domain.com.

When I go to www.domain.com, Chrome reports:

This site can’t be reached
www.domain.com’s server IP address could not be found."

When I try *.domain.com, for instance dog.domain.com or giggles.domain.com, it responds with Bad Request (400). Same responses on MS Edge.

I've followed these links, amongst others:

How To Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 16.04
How To Set Up and Test DNS Subdomains with DigitalOcean's DNS Panel
Subdomains not working with Digital Ocean DNS settings
Why isn't my site available via the www prefix?

I'm trying to deploy a Mezzanine 4.2.3 CMS using Ubuntu 16.04.3, Python 3.5.2, gunicorn, and nginx. I elected not to use the fabfile.py provided by Mezzanine because it didn't work and I couldn't figure out all the things that I had to attend to get it right. It was far easier to get domain.com running with the first DigitalOcean tutorial linked above.

I've placed the IP address, domain.com, *.domain.com, and www.domain.com in ALLOWED_HOSTS in local_settings.py (temporarily; will follow two scoops best practices once I get a better handle on deployment):

ALLOWED_HOSTS = [
    "domain.com",
    "*.domain.com",
    "www.domain.com",
    "IP address",
]

I've also placed them in /etc/nginx/sites-available/project:

server {
listen 80;
server_name [IP address] domain.com *.domain.com www.domain.com;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/project;
}

location / {
include proxy_params;
proxy_pass http://unix:/home/project/project.sock;
}
}

I've added an A record for domain.com pointing to the IP address of the droplet and it's successful. When I go to the IP address or domain.com, the site shows.

Fearing conflicts, I tried these permutations:

Scenario 1:
A:domain.com, *.domain.com

Scenario 2:
A: domain.com, www.domain.com

Scenario 3:
A: domain.com, www.domain.com, *domain.com

Scenario 4:
A: domain.com
CNAME: *.domain.com

Scenario 5:
A: domain.com
CNAME: www.domain.com

Scenario 6:
A: domain.com, *.domain.com
CNAME: *.domain.com

Scenario 7:
A: domain.com, www.domain.com
CNAME: www.domain.com

Scenario 8:
A: domain.com, *.domain.com, www.domain.com
CNAME: *.domain.com

Scenario 9:
A: domain.com, *.domain.com, www.domain.com
CNAME: www.domain.com

Scenario 10:
A: domain.com, *.domain.com, www.domain.com
CNAME: *.domain.com, www.domain.com

Scenario 11:
A: domain.com
CNAME: *.domain.com, www.domain.com

The results were the same for all 11 scenarios. I am leaving things at Scenario 11.

Along the way, I pinged the IP address, domain.com, *.domain.com, and www.domain.com from the command line. It's always successful for the IP address, domain.com, and even *.domain.com. They invariably got a reply from the IP address. However, the response for www.domain.com:

Ping request could not find host www.domain.com. Please check the name and try again.

When I tried host <domain.com/www.domain.com/*.domain.com> +trace, it traced all the way down properly.

When I tried host <domain.com/www.domain.com/*.domain.com> ns1.digitalocean.com, it gave me the correct IP address.

I'm out of ideas. What could I have missed?

1 Answer
alexalexyang January 17, 2018
Accepted Answer

Someone advised me to rework my DNS:

A: example.com
CNAME: *.example.com

And to edit my nginx config:

# Add this server block to rewrite subdomains
server {
listen 80;
server_name *.example.com;
location / {
rewrite ^ http://example.com;
}
}

# Delete *.example.com and www.example.com from this server block
server {
listen 80;
server_name [droplet IP address] example.com;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/example;
}

location / {
include proxy_params;
proxy_pass http://unix:/home/example/example.sock;
}
}

ALLOWED_HOSTS should have: droplet IP address, example.com, and *.example.com.

It worked!

Have another answer? Share your knowledge.