jrief / django-websocket-redis

Websockets for Django applications using Redis as message queue
http://django-websocket-redis.awesto.com/
MIT License
894 stars 221 forks source link

Configuration troubles - nginx & uwsgi #223

Closed LeeKevin closed 7 years ago

LeeKevin commented 7 years ago

Hi!

I'm having issues running 2 vassals in emperor mode (for the app + websocket) behind an nginx server. Everything seems to be running well, but all the websocket requests return error 502 bad gateway. Looks like nginx is preventing the requests to /ws from working...

Running from upstart

exec /usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals/ --logto /var/log/uwsgi.log

With /etc/uwsgi/vassals/dashdb_websocket.ini:

# dashdb_websocket.ini file

[uwsgi]
umask = 002
home = /opt/dashdb/venv/dashdb-3.5
plugin = python35
chdir = /opt/dashdb
master = true
no-orphans = true
die-on-term = true
memory-report = true
env = DJANGO_SETTINGS_MODULE=dashdb.settings.opener
socket = /var/run/uwsgi/dashdb_ws.sock
pythonpath = /opt/dashdb
module = dashdb.wsgi_websocket
threads = 1
processes = 1
http-websockets = true
gevent = 1000
# Log file location
daemonize = /var/log/uwsgi/dashdb_websocket.log

With /etc/uwsgi/vassals/dashdb.ini:

# dashdb.ini file

[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /opt/%n
# Django's wsgi file
module          = dashdb.wsgi
# the virtualenv (full path)
home            = /opt/dashdb/venv/%n-3.5
plugin          = python35
# settings location
env             = DJANGO_SETTINGS_MODULE=dashdb.settings.opener

# Process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 3
# the socket (use the full path to be safe)
socket          = /var/run/uwsgi/%n.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true
# Log file location
daemonize       = /var/log/uwsgi/%n.log

and nginx config:

upstream django_dashdb {
    server unix:/var/run/uwsgi/dashdb.sock;
}

server {
    listen *:80;
    server_name MY_DOMAIN;
    server_tokens off;
    root /opt/dashdb;
    charset utf-8;

    # Increase this if you want to upload large attachments
    # Or if you want to accept large git objects over http
    client_max_body_size 250m;

    # Individual nginx logs for this GitLab vhost
    access_log  /var/log/dashdb/dashdb_access.log;
    error_log   /var/log/dashdb/dashdb_error.log;

    # Django media
    location /media  {
        alias /opt/dashdb/media;
    }

    # Django static
    location /static {
        alias /opt/dashdb/static;
    }

    location / {
        uwsgi_read_timeout 180;
        uwsgi_pass  django_dashdb;
        include     /opt/dashdb/uwsgi_params;
    }

        location /ws/ {
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_pass http://unix:/var/run/uwsgi/dashdb_ws.sock;
    }

    error_page 502 /502.html;
}
LeeKevin commented 7 years ago

I needed to set

http-socket = /var/run/uwsgi/dashdb_ws.sock

instead of

socket = /var/run/uwsgi/dashdb_ws.sock

in the websocket uwsgi config ini

LeeKevin commented 7 years ago

Maybe this needs to be added to the documentation @jrief?