Skip to content

Serve Django web app with nginx + SSL #372

Description

@mhdaouas

Hello,

I want to serve a Django web app with Channels on nginx with SSL and be able to call my Channels websocket with wss:// so I don't get a mixed content error in the Chrome browser. I've been unable to create a reverse proxy (with proxy pass) in the production environment. Can you explain to me clearly how to proceed.

Should I run a daphne instance or can I directly do it just by modifying the nginx config?
Should the daphne instance run with SSL?
What command should I use to run daphne?

When I try the following command:
daphne -e ssl:443:privateKey=/home/.../django/env/lib/python3.9/site-packages/sslserver/certs/development.crt:certKey=/home/.../django/env/lib/python3.9/site-packages/sslserver/certs/development.key apdjango.asgi:application

I get this error:

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

My asgi.py script:

application = ProtocolTypeRouter({
  "http": AsgiHandler(),
  "websocket": AuthMiddlewareStack(
        URLRouter(
            myapp.ws_urls.websocket_urlpatterns
        )
    ),
})

Here is the content of manage.py:

import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'apdjango.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "apdjango.settings")

os.environ['SECRET_KEY'] = 'm%w*qhawjr+!oz$6&^5o%13*08x=qmzfedzdu$i)r)%n#ju@k*'
os.environ.setdefault("SECRET_KEY", "m%w*qhawjr+!oz$6&^5o%13*08x=qmzfedzdu$i)r)%n#ju@k*")

# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'apdjango.settings')
def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'apdjango.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

In my settings.py, I have added these lines:

ASGI_APPLICATION = "apdjango.asgi.application"

CHANNEL_LAYERS = {
    'default': {
        "BACKEND": "channels.layers.InMemoryChannelLayer"
    },
}

Your help is appreciated.
Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions