Skip to content

关于反向代理使用WSS失败的问题

约 176 字小于 1 分钟

2024-11-03

修改前

原先想要用 SSL 代理本地端口用于 wss 连接

location ^~ /wss/ {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Range $http_range;
    	proxy_set_header If-Range $http_if_range;
        proxy_redirect off;
        proxy_pass http://127.0.0.1:3001/;
        
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }

WebSocket connection to 'wss://my_domain/wss?' failed:

解决

参考

去掉 location后 "/wss/" 的 "/"

我们看到请求的地址是 "wss://my_domain/wss?","wss" 后并没有斜杠,要和 location 后的项严格相同

location ^~ /wss {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Range $http_range;
    	proxy_set_header If-Range $http_if_range;
        proxy_redirect off;
        proxy_pass http://127.0.0.1:3001/;
        
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
贡献者: edge-sky