Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Daphne behind nginx

I have a Django app that used uWSGI alongwith Nginx for production deployment. Now I am integrating Channels 2.0 and realised that Daphne is the substitute for uwsgi here. I am able to get Daphne to serve when running it in standalone mode, but when I try to run it behind nginx proxy, i keep getting Connection Timeout (504)

My nginx config file was what I configured for uWSGI server and I am expecting it to just work otb because the socket name is same.

Possible Issues:

  • From what it looks like in the nginx logs, nginx tries to connect to uwsgi://unix:///path/to/nginx.sock but I think it should not be prefixed with uwsgi and should be just unix:///path/to/nginx.sock.

Followup Questions:

  • Is it recommended to use Daphne to server http and websocket both? Or should I be using uWSGI for http and Daphne only for websockets.
  • If using Daphne for both, am I correct in keeping the same uwsgi_params file?

settings.py

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.admin',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_celery_results',
    'home',
    'corsheaders',
    'rest_framework',
    'channels'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware'
]

WSGI_APPLICATION = 'home.wsgi.application'

ASGI_APPLICATION = "home.routing.application"

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [(REDIS_HOST, REDIS_PORT)],
        },
    },
}

asgi.py

import os
import django
from channels.routing import get_default_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "home.settings_dev")
django.setup()
application = get_default_application()

nginx.conf

upstream home {
  server unix:///Users/pranavprakash/workspace/HomeApp/nginx.sock;
}

# configuration of the server
server {
  # the port your site will be served on
  listen      80;
  # the domain name it will serve for
  server_name localhost; # substitute your machine's IP address or FQDN
  charset     utf-8;

  # max upload size
  client_max_body_size 75M;   # adjust to taste

  # Django media
  location /media  {
    alias /Users/pranavprakash/workspace/HomeApp/media;
  }

  location /static {
    alias /Users/pranavprakash/workspace/HomeApp/staticfiles;
  }

  # Finally, send all non-media requests to the Django server.
  location / {
    uwsgi_pass  home;
    include     /Users/pranavprakash/workspace/HomeApp/uwsgi_params;
  }
}

Daphne Server Logs

daphne -u nginx.sock home.asgi:application

2018-06-11 07:09:27,062 INFO     Starting server at unix:nginx.sock
2018-06-11 07:09:27,062 INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2018-06-11 07:09:27,063 INFO     Configuring endpoint unix:nginx.sock

Nginx Logs

2018/06/11 12:41:09 [error] 56711#0: *1 upstream timed out (60: Operation timed out) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///Users/pranavprakash/workspace/HomeApp/nginx.sock", host: "localhost"
2018/06/11 12:41:47 [info] 56711#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection
like image 756
pr4n Avatar asked Jun 11 '18 07:06

pr4n


1 Answers

If I understood correctly, the "home" upstream is not served by uwsgi anymore, try changing uwsgi_pass home; to proxy_pass home;

like image 82
kristaps Avatar answered Oct 01 '22 22:10

kristaps