Question

Django 1-click install 502 bad gateway

I’ve just started with Django’s 1-click install running on Ubuntu 16.04. My IP address works, but the domain doesn’t. I only have IPV4 enabled. I have the correct A record enabled and the NS set up.

But when I access the domain I get 502 bad gateway error. I spun this droplet up last night, so the details should have propagated.

I am getting the following nginx errors in the logs:

2017/01/11 18:11:40 [error] 3002#3002: *1 upstream prematurely closed connection while reading response header from upstream, client: 142.177.130.233, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "my-website.co"
2017/01/11 18:11:41 [error] 3002#3002: *1 upstream prematurely closed connection while reading response header from upstream, client: 142.177.130.233, server: _, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/favicon.ico", host: "my-website.co", referrer: "http://my-website.co/"

I don’t know but I’m assuming this is a Gunicorn issue (I’m new to Gunicorn so don’t know though), my nginx looks like this, I’ve commented things that i’ve added

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
# I added this based off https://www.digitalocean.com/community/tutorials/how-to-use-the-django-one-click-install-image
upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;
}
server {
        listen 80 default_server;
        listen [::]:80 default_server;

# I added this for let's encrypt
location ~ /.well-known {
                allow all;
        }

        # SSL configuration
        #
        # listen 443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php7.0-fpm:
        #       fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}

The link https://www.digitalocean.com/community/tutorials/how-to-use-the-django-one-click-install-image doesn’t seem to have the correct paths for my 1-click, for example it references: /etc/init/gunicorn.conf. Which I don’t seem to have.

I just ran: sudo netstat -plunt | grep gunicorn

And nothing was returned. I assumed this should return something if it was running? I also ran: service gunicorn start and it started once I put my password in, but netstat returns nothing.?

I’m at a bit of a loss here, in where to go and what’s wrong. I kinda expected the one click to go a bit more smoothly. To my knowledge I haven’t done anything that would break it.

Thanks :)

Subscribe
Share

I believe I have been dealing with the same frustrating issue. Any information on this would be amazing.


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

Update Feb 2018:

I had the same problems as you with the one click droplet:

Summary of my solution:

ALLOWEDHOSTS = [ ‘domainname’, ‘dropelt_IP’]

and add the bottom of settings.py:

change: ALLOWEDHOSTS = ipaddresses()

to

ALLOWEDHOSTS += ipaddresses()

Kind regards Moritz

Update Feb 2018:

I had the same problems as you with the one click droplet:

Summary of my solution:

ALLOWEDHOSTS = [ ‘domainname’, ‘dropelt_IP’]

and add the bottom of settings.py:

change: ALLOWEDHOSTS = ipaddresses()

to

ALLOWEDHOSTS += ipaddresses()

Kind regards Moritz

Update Feb 2018:

I had the same problems as you with the one click droplet:

Summary of my solution:

ALLOWEDHOSTS = [ ‘domainname’, ‘dropelt_IP’]

and add the bottom of settings.py:

change: ALLOWEDHOSTS = ipaddresses()

to

ALLOWEDHOSTS += ipaddresses()

Kind regards Moritz

I just did:

sudo systemctl daemon-reload sudo systemctl restart gunicorn

This comment has been deleted

I’m dealing with the same problem. Before trying DO’s Django one-click I was a fan. Now I am very frustrated. It isn’t just one click to make a Django project if their image requires this much debugging!

Hello Jonny, Same here I had to make a new droplet.

But I found an issue why I was getting this error. I think the fault was because nginx wasn’t connecting to gunicorn.

When I replaced system.py file to the one from my Local machine it as well had different security key, (which I think gunicorn catches somewhere and if you replace it doesn’t connect properly).

As well I would recommend when you just starting building your django project stop, gunicorn services. Change nginx port forwording from gunicorn.socket to something like 127.0.0.1:9000 and then run server on 9000 port in this case you will always see the errors and what’s not connecting, later on after development everything could be put back in places.(don’t forget to restart all services after all changes) I followed this tutorial for the changes. https://www.digitalocean.com/community/tutorials/how-to-use-the-django-one-click-install-image

I have the same issue my error log:

2017/01/15 20:32:47 [error] 2104#2104: *340 connect() to unix:/home/django/gunicorn.socket failed (111: Connection refused) while connecting to upstream, client: 82.28.70.53, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "178.62.60.181"
2017/01/15 20:33:17 [error] 2104#2104: *345 connect() to unix:/home/django/gunicorn.socket failed (111: Connection refused) while connecting to upstream, client: 82.28.70.53, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "178.62.60.181"
2017/01/15 20:33:19 [error] 2104#2104: *345 connect() to unix:/home/django/gunicorn.socket failed (111: Connection refused) while connecting to upstream, client: 82.28.70.53, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "178.62.60.181"

nothing seems to help

Here is what fixed this for me based on an answer I found here: https://www.digitalocean.com/community/questions/400-error-bad-request-when-visiting-domain-name-ip-address-loads-fine

in your settings.py file my domain finally worked with the following: Debug = False ALLOWED_HOSTS = [‘*’]

(then found at the bottom of settings.py)

ALLOWED_HOSTS = ip_addresses()

Make those changes in settings.py, then service gunicorn restart to restart gunicorn and voila. I don’t understand the purpose of this line at the bottom but it seems that while debugging you need it uncommented, but while debug=false if it is commented out your domain works. I’ve been working on this in an isolated droplet with django 1 click so the other settings are standard and for me it turned out to not be related to nginx or anything else.

I hope this works for you and anyone else having this trouble