Report this

What is the reason for this report?

Django 1-click install 502 bad gateway

Posted on January 11, 2017

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



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!

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

This comment has been deleted

I just did:

sudo systemctl daemon-reload sudo systemctl restart gunicorn

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.