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

127 天前
 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/
2281 次点击
所在节点    NGINX
30 条回复
263
127 天前
多半是 /ws 这个路径的锅
julyclyde
127 天前
你核实一下,上游的路径也是/ws 吗?
以及:之前看到的 proxy_set_header Connection "Upgrade";这里都是大写
cmmulxuk
127 天前
@263 刚开始没写这个路径,发现所有同步加载的全出问题,GOOGLE 不停搜,最后改成这样还是不行,实在是不知道咋改了。
julyclyde
127 天前
“问 GPT 说是正常的”那说明你思路有问题
你应该思考的是:到底错在哪了
cmmulxuk
127 天前
@julyclyde 使用原网址看了,使用的是 wss://example.com/ws ,大写改了还是不行。
cmmulxuk
127 天前
@julyclyde #4 反复问过 GPT4 了,一直说 看不到明显的错误,代码看起来配置得很好,给我了几个可能,我挨个排查没有发现有问题,折腾几个小时都没搞定,头都搞炸了。
julyclyde
127 天前
@cmmulxuk 原网址是 wss
你现在这个 server 开启 ssl 了吗?
或者你让客户端改为访问 ws 试试?

是客户端报错还是服务器报错啊?发一下浏览器开发者工具里原始的错误信息和服务器日志
julyclyde
127 天前
@cmmulxuk 对啊,这就是个方法问题
你自己没掌握技能,啥都问 GPT ,那怎么可能每次都顺利呢?
cmmulxuk
127 天前
@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',
});
}
```
cmmulxuk
127 天前
@julyclyde #7 全都开启了 SSL ,反代 HTTP 就自动跳回了原网站,不用 HTTPS 也不安全啊,一些服务是需要登录的。
julyclyde
127 天前
@cmmulxuk 浏览器开发者工具看看 network 那个页
cmmulxuk
127 天前
@julyclyde #11 GET /ws HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0
Accept: */*
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate, br
Sec-WebSocket-Version: 13
Origin: https://example.com
Sec-WebSocket-Extensions: permessage-deflate
Sec-WebSocket-Key: eATpcJD9XCDSjNBtoEDjEQ==
DNT: 1
Connection: keep-alive, Upgrade
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: websocket
Sec-Fetch-Site: same-origin
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket



HTTP/1.1 502 Bad Gateway
Server: openresty
Date: Tue, 02 Jan 2024 05:07:08 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
Strict-Transport-Security: max-age=63072000; preload
julyclyde
127 天前
@cmmulxuk bad gateway 看起来应该是 nginx 的问题了
你开启 error_log 看一下对应时刻的内容
把日志级别开得更详细一点
cmmulxuk
127 天前
@julyclyde #13 nginx proxy manager 日志
2024/01/01 14:25:49 [error] 58950#58950: *339383 SSL_do_handshake() failed (SSL: error:1408F10B:SSL routines:ssl3_get_record:wrong version number) while SSL handshaking to upstream, client: 188.188.188.188, server: example.com, request: "GET / HTTP/2.0", upstream: "https://172.17.0.1:8008/", host: "example.com"

2024/01/01 14:25:49 [error] 58950#58950: *339383 SSL_do_handshake() failed (SSL: error:1408F10B:SSL routines:ssl3_get_record:wrong version number) while SSL handshaking to upstream, client: 188.188.188.188 server: example.com, request: "GET /favicon.ico HTTP/2.0", upstream: "https://172.17.0.1:8008/favicon.ico", host: "example.com", referrer: "https://example.com/"
这是 SSL 的问题吗
Lax
127 天前
上游也是 SSL ?? upstream: "https://172.17.0.1:8008/"
Lax
127 天前
这两条日志跟 /ws 路径没关系。
Lax
127 天前
三个 proxy_pass 搞出三种风格:
proxy_pass https://example.com/;
proxy_pass https://example.com;
proxy_pass http://example.com;
NowTime
127 天前
之前反代 wss 遇到过类似的问题,可以看看这篇文章

https://nowtime.cc/news/1844.html
Lax
127 天前
配置里的 upstream 是 443 和 80 端口,日志中的是 upstream 是 8008 端口。
原因就藏在你认为没关系而替换的这几行。
cmmulxuk
127 天前
@Lax #17 proxy_pass 这个其实是手误,原配置三个都是 HTTPS 。
原网站和中转机都配置了 SSL ,原主机配置域名反代 172.17.0.1:8088 ,这是本机配置域名的反代。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1005141

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX