V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
cmmulxuk
V2EX  ›  NGINX

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

  •  
  •   cmmulxuk · 116 天前 · 2200 次点击
    这是一个创建于 116 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想用线路优化机反代大内存机,都是些自己 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/
    
    第 1 条附言  ·  116 天前
    写的头大了,换了方式,使用 WireGuard 组网,反代内网 IP ,直接简单多了。
    30 条回复    2024-01-08 17:53:10 +08:00
    263
        1
    263  
       116 天前
    多半是 /ws 这个路径的锅
    julyclyde
        2
    julyclyde  
       116 天前
    你核实一下,上游的路径也是/ws 吗?
    以及:之前看到的 proxy_set_header Connection "Upgrade";这里都是大写
    cmmulxuk
        3
    cmmulxuk  
    OP
       116 天前
    @263 刚开始没写这个路径,发现所有同步加载的全出问题,GOOGLE 不停搜,最后改成这样还是不行,实在是不知道咋改了。
    julyclyde
        4
    julyclyde  
       116 天前
    “问 GPT 说是正常的”那说明你思路有问题
    你应该思考的是:到底错在哪了
    cmmulxuk
        5
    cmmulxuk  
    OP
       116 天前
    @julyclyde 使用原网址看了,使用的是 wss://example.com/ws ,大写改了还是不行。
    cmmulxuk
        6
    cmmulxuk  
    OP
       116 天前
    @julyclyde #4 反复问过 GPT4 了,一直说 看不到明显的错误,代码看起来配置得很好,给我了几个可能,我挨个排查没有发现有问题,折腾几个小时都没搞定,头都搞炸了。
    julyclyde
        7
    julyclyde  
       116 天前
    @cmmulxuk 原网址是 wss
    你现在这个 server 开启 ssl 了吗?
    或者你让客户端改为访问 ws 试试?

    是客户端报错还是服务器报错啊?发一下浏览器开发者工具里原始的错误信息和服务器日志
    julyclyde
        8
    julyclyde  
       116 天前
    @cmmulxuk 对啊,这就是个方法问题
    你自己没掌握技能,啥都问 GPT ,那怎么可能每次都顺利呢?
    cmmulxuk
        9
    cmmulxuk  
    OP
       116 天前
    @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
        10
    cmmulxuk  
    OP
       116 天前
    @julyclyde #7 全都开启了 SSL ,反代 HTTP 就自动跳回了原网站,不用 HTTPS 也不安全啊,一些服务是需要登录的。
    julyclyde
        11
    julyclyde  
       116 天前
    @cmmulxuk 浏览器开发者工具看看 network 那个页
    cmmulxuk
        12
    cmmulxuk  
    OP
       116 天前
    @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
        13
    julyclyde  
       116 天前
    @cmmulxuk bad gateway 看起来应该是 nginx 的问题了
    你开启 error_log 看一下对应时刻的内容
    把日志级别开得更详细一点
    cmmulxuk
        14
    cmmulxuk  
    OP
       116 天前
    @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
        15
    Lax  
       116 天前
    上游也是 SSL ?? upstream: "https://172.17.0.1:8008/"
    Lax
        16
    Lax  
       116 天前
    这两条日志跟 /ws 路径没关系。
    Lax
        17
    Lax  
       116 天前
    三个 proxy_pass 搞出三种风格:
    proxy_pass https://example.com/;
    proxy_pass https://example.com;
    proxy_pass http://example.com;
    NowTime
        18
    NowTime  
       116 天前 via Android
    之前反代 wss 遇到过类似的问题,可以看看这篇文章

    https://nowtime.cc/news/1844.html
    Lax
        19
    Lax  
       116 天前
    配置里的 upstream 是 443 和 80 端口,日志中的是 upstream 是 8008 端口。
    原因就藏在你认为没关系而替换的这几行。
    cmmulxuk
        20
    cmmulxuk  
    OP
       116 天前
    @Lax #17 proxy_pass 这个其实是手误,原配置三个都是 HTTPS 。
    原网站和中转机都配置了 SSL ,原主机配置域名反代 172.17.0.1:8088 ,这是本机配置域名的反代。
    cmmulxuk
        21
    cmmulxuk  
    OP
       116 天前
    @NowTime 这两行已经加了的,还是没用
    magicZ
        22
    magicZ  
       116 天前
    upstream proxy102 {
    server localhost:9999;
    }

    server {
    listen 3478 ssl http2 default_server;
    server_name xxx.yyy.com;
    ssl_certificate /etc/nginx/keys/xxx.pem;
    ssl_certificate_key /etc/nginx/keys/xxx.key;

    location /wss {
    proxy_pass http://proxy102; #代理到上面的地址去
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_read_timeout 86400;
    proxy_send_timeout 86400;
    }
    }
    poopoopoopoo
        23
    poopoopoopoo  
       116 天前
    是不是 upstream 写错了

    http://172.17.0.1:8008 不能加 S

    listen 8008 ssl;才能加 S
    Lax
        24
    Lax  
       116 天前
    @cmmulxuk 如果理解这几行的差异,就不会写出这样的失误。想解决问题先拿张纸画一下部署的逻辑结构吧,不是随便抓住一根稻草就能救命。
    kokojack
        25
    kokojack  
       116 天前
    我的建议是,改用 lucky 反代,一开始也是用 nginx ,nginxwebui 之类的,现在用 lucky ,非常舒服,什么配置都不用写,非常强大
    julyclyde
        26
    julyclyde  
       115 天前
    @kokojack 你这个建议没啥用
    找到真实原因之前,换软件应该不会有什么区别
    ResidualBlood
        27
    ResidualBlood  
       115 天前
    先用 官方 给的配置看看正常不
    哪吒探针 https://nezha.wiki/guide/dashboard.html#%E9%85%8D%E7%BD%AE%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86
    我用的 这个 反代是正常的
    cmmulxuk
        28
    cmmulxuk  
    OP
       115 天前
    @ResidualBlood 我不是本机反代,我是反代的基础上再套一层反代 HTTPS ,优化线路。一直失败,估计是 VPS 哪里有问题没找出来,直接改其他方式了。
    chaoschick
        29
    chaoschick  
       115 天前 via Android
    location 的位置需要换一下 /ws 要在上面 /要在最后面 不然/会直接拦截掉/ws 的流量
    kokojack
        30
    kokojack  
       110 天前
    @julyclyde #26 不会啊,用 Lucky 不需要写配置默认全加上,而且不冲突。无影响。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   869 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 20:49 · PVG 04:49 · LAX 13:49 · JFK 16:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.