1

I'm having problems to deploy this in a production virtual machine, with Nginx + Gunicorn + Daphne + Django. I had been testing it in a local virtual machine, and it works without a problem, but in production, the sockets is connecting and disconnecting. I attached my nginx config, asgi.py and routing.py. I use the command ````$ daphne -p 8010 project.asgi:application``` enter image description here

# Nginx config
upstream test_project {
        server localhost:8001;
}
upstream test_project_websocket {
        server localhost:8002;
}
server {
        listen 1881;

        location / {
                proxy_pass http://test_project;
        }
        location /ws/ {
                proxy_pass http://test_project_websocket;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
                proxy_redirect off;
        }

        proxy_set_header Host $host;
}

#asgi.py
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'WebServer.settings')
django.setup()

from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter
import Andon.routing # app.routing
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.layers import get_channel_layer


application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    "websocket": AuthMiddlewareStack(
            URLRouter(
                    Andon.routing.websocket_urlpatterns,
            ),
        ),
})
# routing.py
from django.urls import path, re_path
from . import consumers

from channels.layers import get_channel_layer

websocket_urlpatterns = [
    re_path(r'ws/andon/$', consumers.AndonConsumer.as_asgi()),
]

Edit 27-11-20: Didn't solvet yet, but found something interesting. If you are using old versions of redis, make sure you have this versions:

channels==3.0.2
channels-redis==2.4.1

Edit 27-11-20: partial solve I think i'm having some problems with the nginx config, because if i try to connect directly to the daphne port, it works just fine, but if i redirect trafic from nginx it doesn't

Edit 16-12-20: Solution I had an old version of nginx 1.10, I upgrade it to 1.16 and worked without any problem, using the config of this post: Config

1 Answers1

1

This is the config your looking for

# Connecting to daphne socket
upstream test_project_websocket {
        server localhost:8010;
}
....
    # Notice the "/" at then end of location & proxy_pass url
    location /ws/ {
        proxy_pass http://test_project_websocket/;

        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;
    }

    location / {
         proxy_pass http://test_project;
    }

Arash77
  • 613
  • 1
  • 5
  • 7
  • I notice that in the original post i wrote the wrong port, but in the deployment i wrote it fine. I try your answer, didn't work, i check the "/" that you point out, also try to remove the originvalidators from the asgi routing, but it didn't work. I will keep searching info – lautaro dapin Nov 26 '20 at 11:34
  • is your nginx 1881 port running with ssl? can you put a code of the client and how your are trying to connect ot the server? also a screen of daphne console and any warnings it raises on connect would be nice to debug the problem. – Arash77 Nov 28 '20 at 07:48
  • I've tried everything, and nothing has worked, until I saw this config. I noticed that you put /ws/ config above / config. I tested this and et voila! after 1 week of struggle , everything works now :) – Gomeisa Dec 06 '21 at 06:24