Running daphne in production - Mac OS X Server 10.11

442 views
Skip to first unread message

Adam Teale

unread,
Nov 10, 2016, 3:25:12 PM11/10/16
to django...@googlegroups.com
Hi everyone,

I am looking into Andrew Godwin's example project "channels-example" (https://github.com/andrewgodwin/channels-examples).

The multichat demo runs fine using the development/localhost server and now I am attempting to put it into production mode so I can access across our network and test it out.

I have my own django app that I have running on Mac OS X server using the Apache module mod_wsgi .
I have used this same setup to get the multi chat demo running.

I do understand that all http and web socket traffic can be handled by Daphne and that it isn't necessary to run the app via WSGI. 

I can access the multi chat app from a browser in another machine however when it comes to the part of the the app connecting via web sockets I get this error:

    WebSocket connection to 'wss://myexampleserver.local/chat/stream/' failed: Unexpected response code: 404

I have Daphne running like this:

daphne multichat.asgi:channel_layer


The terminal returns:

Starting server at 127.0.0.1:8000, channel layer multichat.asgi:channel_layer

Using busy-loop synchronous mode on channel layer


The worker is running too via python manage.py runworker

My feeling is that I do not have Daphne configured properly to receive the web socket connections from the appropriate port / IP address (it shouldn't be localhost right?).

I hope that makes some sense and that someone might be able to shed some light on what I have misunderstood.

Many thanks!

Adam



Andrew Godwin

unread,
Nov 10, 2016, 3:33:32 PM11/10/16
to django...@googlegroups.com
Hi Adam,

It looks like you're running the main site through mod_wsgi, which means that it does not support WebSockets; daphne won't help as you're running it on a different port. Either you need to access the whole site via Daphne, or change the code so that it appends a different port to the WebSocket source URL (by default it's trying the same port as the page it's served on)

Andrew

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMse0ZhCxubi-EWgux9Xah%2BRd_JOSWHDW0HgqgvkZtWr%3DPkDAg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Adam Teale

unread,
Nov 10, 2016, 3:48:41 PM11/10/16
to Django users
Hi Andrew,

Thank you very much for you help.

Can you direct me to where I might be able to find some info about configuring Daphne for production? 

I've gone through what you have available on the Read the docs (https://channels.readthedocs.io/en/stable/deploying.html) however I think I need some more basic information on setting up the server itself so that Daphne is doing all the work - or perhaps I am missing something completely obvious here.

Thanks again Andrew!

Adam
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Andrew Godwin

unread,
Nov 10, 2016, 3:53:18 PM11/10/16
to django...@googlegroups.com
Hi Adam,

That document is the totality of what I have personally written, so I can't direct you to others. If we can work out what you're missing we can add it.

Basically, all you need to do is:

* Run Daphne to listen on a port (8000 by default, but you can pick)
* Make your frontend webserver (sounds like Apache for you) proxy all requests to this port
* Optionally: Make apache either proxy or use mod_wsgi based on path prefix

Andrew

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Adam Teale

unread,
Nov 10, 2016, 4:00:22 PM11/10/16
to Django users
Ahhh great that's probably what I was misunderstanding - setting up Apache to proxy all requests to Daphne.

I'll look into this and post back to this thread just in case anyone else is in the same boat.

Cheers Andrew!

Adam

Andrew

Adam Teale

unread,
Nov 11, 2016, 9:24:25 AM11/11/16
to Django users
Hi Andrew,

I think I'm starting to get somewhere, I have edited one of the apache virtualhost conf files to include these lines:

    ProxyPass / http://127.0.0.1:8000/
    ProxyPassReverse / http://127.0.0.1:8000/

Now when I access the server I am getting some feedback in the steel running the worker and the shell running Daphne

I continue to get the "Not Found: /chat/stream/" in the worker as well as this in the browser:

    WebSocket connection to 'ws://alfred.local/chat/stream/' failed: Unexpected response code: 404

I'll keep looking. Cheers!

Adam Teale

unread,
Nov 11, 2016, 9:25:17 AM11/11/16
to Django users
Also getting this error in the browser log:

    WebSocket connection to 'ws://alfred.local/chat/stream/' failed: Unexpected response code: 503

Adam Teale

unread,
Nov 11, 2016, 10:00:45 AM11/11/16
to Django users
I think my grief may be coming from connecting over https/SSL. 

When I run the worker with the -v 2 option it shows:

2016-11-11 14:55:43,446 - DEBUG - worker - Got message on http.request (reply http.response!EKDQPeJsDRwU)

2016-11-11 14:55:43,447 - DEBUG - runworker - http.request

2016-11-11 14:55:43,447 - DEBUG - worker - Dispatching message on http.request to channels.staticfiles.StaticFilesConsumer

Not Found: /chat/stream/


Any ideas?

Adam Teale

unread,
Nov 11, 2016, 10:45:10 AM11/11/16
to Django users
I came across a post that suggested running Daphne like this:

    daphne multichat.asgi:channel_layer --port 8000 -v 2


The updated error:


2016-11-11 15:43:14,453 DEBUG    HTTP GET request for http.response!KwJtQDgmoifR

2016-11-11 15:43:14,478 DEBUG    HTTP 404 response started for http.response!KwJtQDgmoifR

2016-11-11 15:43:14,478 DEBUG    HTTP close for http.response!KwJtQDgmoifR

2016-11-11 15:43:14,478 DEBUG    HTTP response complete for http.response!KwJtQDgmoifR

127.0.0.1:51749 - - [11/Nov/2016:15:43:14] "GET /chat/stream/" 404 2279


It seems the error is coming from not being able to locate the route/path "/chat/stream/"


Andrew Godwin

unread,
Nov 11, 2016, 11:13:21 AM11/11/16
to django...@googlegroups.com
That's not the problem - if an incoming connection is a WebSocket it does not run through the URL resolver (as you are seeing there) but instead gets upgraded on the wire and dispatched differently.

A quick read up makes it seem like the Apache proxy module does not understand WebSocket (it's a binary protocol upgraded from HTTP mid-stream, so it requires some intelligence on the part of the proxy server), and so it's ending up as normal HTTP. I've not used Apache in many years so I'm afraid I don't know how to solve this easily - it looks like there might be a mod_proxy_wstunnel you can use, though.

Andrew

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Adam Teale

unread,
Nov 14, 2016, 8:45:41 AM11/14/16
to Django users
Unfortunately I haven't had any luck with it.

I have the mod_proxy and mod_proxy_wstunnel Apache modules loaded and I the following proxies inside the virtual host for port 443 (SSL)

    ProxyPass /chat/stream/ wss://127.0.0.1:8000/chat/stream/
    ProxyPassReverse /chat/stream/ wss://127.0.0.1:8000/chat/stream/

    ProxyPass /ws  ws://127.0.0.1:8000/
    ProxyPassReverse /ws wss://127.0.0.1:8000/
    ProxyPass /wss wss://127.0.0.1:8000/
    ProxyPassReverse /wss wss://127.0.0.1:8000/

    
    ProxyPass / http://127.0.0.1:8000/
    ProxyPassReverse / http://127.0.0.1:8000/


If anyone can shed some light on this or offer an opinion I'd really appreciate it.

Cheers

Adam
Reply all
Reply to author
Forward
0 new messages