Unable to get Django Channels Daphne Nginx Supervisor to run: 502 bad gateway

Posted February 28, 2019 7.7k views
NginxDjangoUbuntu 16.04

I’m trying to traceback through the errors to find the core problem as to why my domain is displaying a 502 bad Gateway error.

I’m running a Django app using Python3+ on Digital Ocean Ubuntu 16.04 using Nginx and supervisor (which manages daphne because ASGI is needed for Django Channels).

I just can’t get it to work, I must be missing something.

Both Nginx and Supervisor are running.

The nginx error log shows:

 connect() failed (111: Connection refused) while connecting to upstream, client:, server:, request: "GET / HTTP/1.1", upstream: "", host: ""

Running sudo /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf

2019-02-27 16:39:55,627 CRIT Supervisor running as root (no user in config file)
2019-02-27 16:39:55,627 WARN Included extra file "/etc/supervisor/conf.d/myapp_asgi.conf" during parsing
2019-02-27 16:39:55,629 INFO Creating socket tcp://
2019-02-27 16:39:55,629 INFO Closing socket tcp://
Error: Another program is already listening on a port that one of our HTTP servers is configured to use.  Shut this program down first before starting supervisord.

my nginx conf file

upstream myapp {

server {

    location = /favicon.ico { access_log off; log_not_found off; }

    location / {
        try_files $uri @proxy_to_app;

    location /static/ {
        root /home/me/myapp/src/myapp;

    location /media/  {
        root /home/me/myapp/src/myapp;
        include /etc/nginx/mime.types;

    location @proxy_to_app {
        proxy_pass http://myapp;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


server {
    if ($host = {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    return 404; # managed by Certbot

my supervisor config file


# TCP socket used by Nginx backend upstream

# Directory where your site's project files are located

# Each process needs to have a separate socket file, so we use process_num
# Make sure to update "mysite.asgi" to match your project name
command=/home/me/Env/myapp/bin/daphne -u run/daphne/daphne%(process_nu$

# Number of processes to startup, roughly the number of CPUs you have

# Give each process a unique name so they can be told apart

# Automatically start and recover processes

# Choose where you want your log to go

Please if someone could look over this, I’ve spend days with no progress.

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.

Submit an Answer
1 answer

Did you register your domain to allowed hosts in

  • Hey I currently have my allowed hosts as [*] so it should allow everything right now.

    • Hey, I have set it up with my specific domain names.
      In addition, at the bottom of the your code should look like this.

      # Find out what the IP addresses are at run time
      # This is necessary because otherwise Gunicorn will reject the connections
      def ip_addresses():
          ip_list = []
          for interface in netifaces.interfaces():
              addrs = netifaces.ifaddresses(interface)
              for x in (netifaces.AF_INET, netifaces.AF_INET6):
                  if x in addrs:
          return ip_list
      # Discover our IP address
      ALLOWED_HOSTS += ip_addresses()

      Not setting up your allowed hosts appropriately is a common issue when setting up a django project on digital ocean, thats why i am telling you. Making sure that this is set up correctly is important, but I cannot help you with your specific setup, since I dont work with Django Channels Daphne Nginx Supervisor.

      Kind regards

      Marcel Kresse

      • I’m actually not using Gunicorn, only Daphne and Nginx, this is why it’s been so difficult to find tutorials as most people are using Gunicorn as well. Anyways, thanks for your help, I will keep this in mind if I end up using it.

  • Does my code look correct?