与Nginxredirect的Django频道

我为我的Django应用程序使用Django频道。 在Django之上,我添加了nginx作为http请求的层。 所有的http请求都运行良好,但是当我尝试创buildwebsocket连接时,得到了302 HTTP代码。

Nginxconfiguration

# Enable upgrading of connection (and websocket proxying) depending on the # presence of the upgrade field in the client request header map $http_upgrade $connection_upgrade { default upgrade; '' close; } # Define connection details for connecting to django running in # a docker container. upstream uwsgi { server uwsgi:8080; } server { # OTF gzip compression gzip on; gzip_min_length 860; gzip_comp_level 5; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain application/xml application/x-javascript text/xml text/css application/json; gzip_disable “MSIE [1-6].(?!.*SV1)”; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; # the port your site will be served on listen 8080; # the domain name it will serve for #server_name *; charset utf-8; error_page 500 502 /500.html; location = /500.html { root /html; internal; } # max upload size, adjust to taste client_max_body_size 15M; # Django media location /media { # your Django project's media files - amend as required alias /home/web/media; expires 21d; # cache for 71 days } location /static { # your Django project's static files - amend as required alias /home/web/static; expires 21d; # cache for 21 days } location /archive { proxy_set_header Host $http_host; autoindex on; # your Django project's static files - amend as required alias /home/web/archive; expires 21d; # cache for 6h } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass uwsgi; # the uwsgi_params file you installed needs to be passed with each # request. # the uwsgi_params need to be passed with each uwsgi request uwsgi_param QUERY_STRING $query_string; uwsgi_param REQUEST_METHOD $request_method; uwsgi_param CONTENT_TYPE $content_type; uwsgi_param CONTENT_LENGTH $content_length; uwsgi_param REQUEST_URI $request_uri; uwsgi_param PATH_INFO $document_uri; uwsgi_param DOCUMENT_ROOT $document_root; uwsgi_param SERVER_PROTOCOL $server_protocol; uwsgi_param HTTPS $https if_not_empty; uwsgi_param REMOTE_ADDR $remote_addr; uwsgi_param REMOTE_PORT $remote_port; uwsgi_param SERVER_PORT $server_port; uwsgi_param SERVER_NAME $server_name; if (!-f $request_filename) { proxy_pass http://uwsgi; break; } # Require http version 1.1 to allow for upgrade requests proxy_http_version 1.1; # We want proxy_buffering off for proxying to websockets. proxy_buffering off; # http://en.wikipedia.org/wiki/X-Forwarded-For proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # enable this if you use HTTPS: # proxy_set_header X-Forwarded-Proto https; # pass the Host: header from the client for the sake of redirects proxy_set_header Host $http_host; # We've set the Host header, so we don't need Nginx to muddle # about with redirects proxy_redirect off; # Depending on the request value, set the Upgrade and # connection headers proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; } } 

Routing.py

 from channels.routing import route from consumers import ws_add, ws_message, ws_disconnect channel_routing = [ route("websocket.connect", ws_add), route("websocket.receive", ws_message), route("websocket.disconnect", ws_disconnect), ] 

消费者。 PY

 from channels import Channel, Group from channels.sessions import channel_session from channels.auth import channel_session_user, channel_session_user_from_http # Connected to websocket.connect @channel_session_user_from_http def ws_add(message): # Accept the connection message.reply_channel.send({"accept": True}) # Add to the group Group("progress-%s" % message.user.username).add(message.reply_channel) @channel_session_user def ws_message(message): Group("progress-%s" % message.user.username).send({ "text": message['text'], }) # Connected to websocket.disconnect @channel_session_user def ws_disconnect(message): Group("progress-%s" % message.user.username).discard(message.reply_channel) 

如果我删除nginx图层,它工作得很好。 有没有我想念的configuration?