django➕dwebsocket➕nginx报错301怎么解决?

最近在做网站的站内消息,使用的django➕dwebsocket➕nginx
但是在上到服务器的时候,访问建立连接的函数,报错301永久重定向了,我nginx也配置了。但是就是连接不上。

nginx配置如下
 server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        # gzip config
        gzip on;
        gzip_min_length 1k;
        gzip_comp_level 9;
        gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
        gzip_vary on;
        gzip_disable "MSIE [1-6]\.";
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location /api {
                rewrite ^/api/(.*)$ /$1 break;
                proxy_pass http://39.97.166.114:8000;
    proxy_http_version 1.1; 
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_connect_timeout 60;
                proxy_read_timeout 600;
             proxy_send_timeout 600;
        }

后台代码
clients = []
@accept_websocket
def echo(request):
    if request.is_websocket:
        try:
            clients.append(request.websocket)
            print(clients)
            for message in request.websocket:
                print(message)
                me = eval(message)  # 将字符串类型的消息转换为字典型
                print(type(me))
                print(me["name"])
                me['id']=1
                me['username']=2
                print(me)
                # print(request.websocket.count_messages())
                if not message:
                    break
                for client in clients:
                    # print(client.count_messages())
                    print(client)
                    # print(client.has_messages())
                    client.send(message)
        finally:
            clients.remove(request.websocket)

前端代码
<!DOCTYPE html>
<html>
<head>
    <title>django-websocket</title>
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script type="text/javascript">//<![CDATA[
    $(function () {
        //创建WebSocket对象获得连接
        $('#connect_websocket').click(function () {
            if(window.websocket){
                window.websocket.close()
            }
            var websocket = new WebSocket("ws://saas111.com/api/user/echo");
            websocket.onopen = function () {
                console.log('WebSocket open');
            };
            websocket.onmessage = function (responseData) {
                console.log('message: ' + responseData.data);//浏览器控制台打印返回的数据
                var jsondata = eval('(' + responseData.data + ')'); //将返回的数据转换为json对象
                $('#messagecontainer').prepend('<p>' + jsondata.name + '</p>'); //展示数据
            };
            window.websocket = websocket;
        });
        //发送消息
        $('#send_message').click(function () {
            if(!window.websocket){
                alert("Please connect server.");
            }else{
{#                window.s.send($('#message').val());#} //发送字符串数据类型
                window.websocket.send(JSON.stringify({"name":$('#message').val()}));//发送json数据类型
            }
        });
        //关闭websocket连接
        $('#close_websocket').click(function () {
            if(window.websocket){
                window.websocket.close();
            }
        });

    });
    //]]></script>
</head>
<body>
<br>
<input type="text" id="message" value="Hello, World!" />
<button type="button" id="connect_websocket">连接websocket</button>
<button type="button" id="send_message">发送消息</button>
<button type="button" id="close_websocket">关闭websocket</button>
<h1>接收消息</h1>
<div id="messagecontainer">

</div>
</body>
</html>

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问