nginx 设置 wss:// 持续获取 301 重定向

nginx 设置 wss:// 持续获取 301 重定向

无法在我的 Digital Ocean、Ubuntu 服务器中使用 nginx 来让 wss://(或 ws://)工作,一直出现 301 重定向并且无连接。

Websocket 服务器:node + express + uws 提供服务http://localhost:3000/聊天(我已经通过在 ufw 中打开 3000 并直接使用 ws:// 进行连接进行了测试,运行正常。)

操作系统:Ubuntu 16.04.3 x64

这是我的 nginx 配置(我尝试过很多变体和选项,这是最简单的,老实说,这似乎并不重要)

server {
    listen 443 ssl; # client_wss_port
    server_name www.example.org;

    ssl on;
    ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem;


    location /chat/ {
        add_header locationischat 1; # this is a dummy header for debugging
        proxy_pass http://localhost:3000/chat/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

这是我的 nginx 版本

nginx -v
nginx version: nginx/1.10.3 (Ubuntu)

这是我的防火墙状态

ufw status
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                  
80                         ALLOW       Anywhere                  
443                        ALLOW       Anywhere                  
8888                       DENY        Anywhere                  
3000                       DENY        Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)             
80 (v6)                    ALLOW       Anywhere (v6)             
443 (v6)                   ALLOW       Anywhere (v6)             
8888 (v6)                  DENY        Anywhere (v6)             
3000 (v6)                  DENY        Anywhere (v6) 

以下是请求/响应的示例(使用 Chrome 上的 Smart Websocket Client 插件)

Request URL: wss://www.example.org/chat
Request Method: GET
Status Code: 301 Moved Permanently
Connection: keep-alive
Content-Length: 194
Content-Type: text/html
Date: Wed, 23 May 2018 10:42:35 GMT
Location: https://www.example.org/chat/
locationischat: 1
Server: nginx/1.10.3 (Ubuntu)
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: no-cache
Connection: Upgrade
Host: www.example.org
Origin: chrome-extension://omalebghpgejjiaoknljcfmglgbpocdp
Pragma: no-cache
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Key: hFvk0oEAzI5FLVd4W2fgoA==
Sec-WebSocket-Version: 13
Upgrade: websocket
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36

以下是上述请求的 nginx access.log

49.195.190.75 - - [23/May/2018:22:39:16 +0000] "GET /chat HTTP/1.1" 301 194 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36"

答案1

您的location指令指的是/chat/,但您尝试使用的端点是/chat

因为location存在具有相同路径的 a ,但/附加了 a ,所以 nginx 会生成内部重定向。

如果/chat是 WebSocket 端点,那么您应该使用location /chat

您可能缺少其他一些必要的标头。以下是您应该拥有的内容的一个工作示例:

    location /ws {
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header Proxy "";
            proxy_set_header Host $http_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-Proto $scheme;
            proxy_pass http://localhost:8080;
    }

相关内容