Django NGINX configuration with DNS

January 1, 2015 5.2k views

Hi,

I used the one click django install to create a droplet for my blog. I have succesfully gotten the site to run, and it loads well via the the IP Address (http://104.236.117.75/). However, when I use my domain name (johnpham.me), I get a Bad Request (400) error. I purchased the domain from namecheap and redirected it to use the DO nameservers. I set it to @ for the ip address. I was thinking it was a DNS propagation issue, but whois lookups for the domain shows the digitalocean nameservers. Pinging the domain name works. Domain lookups reveal the correct IP, but I still can't visit the site from the domain name.

I've configured my allowed hosts ["*"] in settings.py to see if I can get anything to work.
Here's the relevant part of my NGINX settings:
Could the root, index, upstream app server be the problem? Those don't refer to anything that I'm currently using, but since it loads fine with the IP address I didn't think I would need to delete it.

upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name johnpham.me;

    keepalive_timeout 5;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }


Any help or insights would really be appreciated!

5 Answers

A hostname, in most standard settings, would be in the following format:

host . yourdomain . ext

So as an example, if I owned `digitalocean.com, and I created two new Droplets and named them ocean and waves, my hostnames would be like so:

ocean.digitalocean.com

waves.digitalocean.com

It's best to keep them simple and straight-forward unless you're managing a huge server-farm and are on the brink of exhausting naming options, thus being forced to move to a second . to give you more options & naming combination.

So for yours, it looks like a .me domain was attached to the hostname. Were it an actual domain you were trying to use, it would not resolve, but it would resolve to xyz.me.johnpham.me, but not xyz.me alone.

You'd need to stick with a valid format as invalid hostnames & domains can cause some issues, so we'd be looking at the following for a hostname xyz using the domainjohnpham.me:

127.0.0.1 xyz.johnpham.me johnpham.me
127.0.0.1 localhost

Between the 127.0.01 and xyz.johnpham.met there is one space bar tap, or a single space. Likewise, the same is true between xyz.johnpham.me and johnpham.me and on the second line between 127.0.0.1 and localhost.

After this is set, you'll need to restart Apache, or better yet, I'd simply reboot the server entirely using:

shutdown -r now

(meaning shutdown for restart, now!)

This way the server, the networking, apache...everything gets a fresh start. If you are still getting the error, I would check the error log to see if there's some information inside that can help uncover this issue.

Some have reported issues with using the WildCard Selector (i.e. * ), so you may want to try using the domain(s) again once the server is rebooted and back in a live, active state.

  • So in a desparate, frustrated attempt to fix this issue, I created an image and then did a fresh re-configuration. After the re-config didn't work, I restored from the previous image. And everything appeared to work again. So in essence, I think this was the equivalent of restarting the server.

    Thanks again for your help!

@johnpham92

Well that wasn't what I meant to do :). Accidentally deleted my comment instead of tagging you. Sorry about that. Let's try this again!

Open up /etc/hosts and make sure you have your hostname & domain listed, and they are resolving.

127.0.0.1 host.johnpham.me johnpham.me
127.0.0.1 localhost

Change host to your Droplets hostname.

Beyond that, just for the sake of keeping order, see the comments in the server { definition :). Just to keep things in order and to help clean up the actual domain server configuration.

server {

    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    #
    # server_name *should* be below listen, but
    # it's not what's causing the issue. Just a
    # nitpick to keep things in order.
    #
    server_name johnpham.me;

    root /usr/share/nginx/html;
    index index.html index.htm;

    #
    # Move to /etc/nginx.conf
    #
    client_max_body_size 4G;
    keepalive_timeout 5;
    #
    # End
    #

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_pass http://app_server;
        }

}
  • @jtittle

    Thanks for answering. So just to clarify if my hostname was xyz.me, it should read:

    127.0.0.1 xyz.me.johnpham.me johnpham.me
    127.0.0.1 localhost
    

    I made the proper adjustments and am stilling getting the same 400 error.
    Not sure why this isn't working if the IP address appears to be working fine.

Did you ever figure this out? I'm having the same issue

For the records:
Be sure about the configuration of allowed hosts in Django Settings!

I wasted some hours to figure out that I forgot to also add the domain name there, so:

ALLOWEDHOSTS = [serverip, domain1.com, domain2.com]

Have another answer? Share your knowledge.