NGINX 反代 ws 总是失败,麻烦大佬帮忙看下配置

查看 84|回复 9
作者:cmmulxuk   
想用线路优化机反代大内存机,都是些自己 Docker 的小工具,emby 、miniflux 、哪吒面板之类的。
都是使用的 Nginx Proxy Manager ,原网站一切正常,用了 SSL ,反代 HTTPS 。
最后在 websockets 这里卡住了,websocket support 打开了,所有的同步加载的东西全都无法连接,哪吒面板“实时同步通断断开”,lobehub 接收完了之后再一起回复,而不是一个字一个字的往外蹦。
问 chatgpt4 说我配置正常的,GOOGLE 也查不到解决方案,麻烦大佬帮我看下配置是不是有问题。
#PROXY-START/
client_max_body_size 100M;
    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_set_header Sec-WebSocket-Extensions $http_sec_websocket_extensions;
    proxy_set_header Sec-WebSocket-Key $http_sec_websocket_key;
    proxy_set_header Sec-WebSocket-Version $http_sec_websocket_version;
    proxy_cache off;
    proxy_redirect off;
    proxy_buffering off;
location / {
        proxy_ssl_server_name on;
        proxy_ssl_name example.com;
        proxy_ssl_protocols TLSv1.2 TLSv1.3;
        proxy_pass https://example.com/;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host example.com;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
location ~* \.(gif|png|jpg|css|js|woff|woff2)$
{
        proxy_ssl_server_name on;
        proxy_ssl_name example.com;
        proxy_ssl_protocols TLSv1.2 TLSv1.3;
        proxy_pass https://example.com;
    proxy_set_header Host example.com;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
    expires 1h;#图片缓存浏览器过期时间
}
location /ws {
    proxy_pass http://example.com;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header Origin $scheme://$host;
    proxy_cache_bypass $http_upgrade;
}
#PROXY-END/

proxy_set_header, Upgrade, tlsv, host

263   
多半是 /ws 这个路径的锅
julyclyde   
你核实一下,上游的路径也是/ws 吗?
以及:之前看到的 proxy_set_header Connection "Upgrade";这里都是大写
cmmulxuk
OP
  
@263 刚开始没写这个路径,发现所有同步加载的全出问题,GOOGLE 不停搜,最后改成这样还是不行,实在是不知道咋改了。
julyclyde   
“问 GPT 说是正常的”那说明你思路有问题
你应该思考的是:到底错在哪了
cmmulxuk
OP
  
@julyclyde 使用原网址看了,使用的是 wss://example.com/ws ,大写改了还是不行。
cmmulxuk
OP
  
@julyclyde #4 反复问过 GPT4 了,一直说 看不到明显的错误,代码看起来配置得很好,给我了几个可能,我挨个排查没有发现有问题,折腾几个小时都没搞定,头都搞炸了。
julyclyde   
@cmmulxuk 原网址是 wss
你现在这个 server 开启 ssl 了吗?
或者你让客户端改为访问 ws 试试?
是客户端报错还是服务器报错啊?发一下浏览器开发者工具里原始的错误信息和服务器日志
julyclyde   
@cmmulxuk 对啊,这就是个方法问题
你自己没掌握技能,啥都问 GPT ,那怎么可能每次都顺利呢?
cmmulxuk
OP
  
@julyclyde #8 服务器日志显示的都是这种无效信息
```
2024/01/02 03:54:29 [notice] 1206#1206: signal process started
2024/01/02 04:29:23 [notice] 1264#1264: signal process started
2024/01/02 04:35:12 [notice] 1274#1274: signal process started
2024/01/02 04:35:30 [notice] 1284#1284: signal process started
2024/01/02 04:39:27 [notice] 1294#1294: signal process started
```
浏览器报错:
```
WebSocket connection to 'wss://example.com/ws' failed:
connect @ (索引):685
function connect() {
const wsProtocol = window.location.protocol == "https:" ? "wss" : "ws"
const ws = new WebSocket(wsProtocol + '://' + window.location.host + '/ws');
ws.onopen = function (evt) {
canShowError = true;
$.suiAlert({
title: '实时通道已建立',
description: '可以实时获取最新监控数据啦',
type: 'success',
time: '2',
position: 'top-center',
});
}
```
您需要登录后才可以回帖 登录 | 立即注册

返回顶部