wss连接不上,https可以访问

Morning

wss连接不上

大佬们,我现在用的nginx代理,用https可以直接访问,但wss就是不通。。。
Nginx1.22 PHP7.2 workerman4.1
截图

截图

截图

程序代码

这是nginx配置

server
{
    listen 80;
    listen 443 ssl http2;
    server_name www.aaa.com;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/aaa/public;

    ssl_certificate    /www/server/panel/vhost/cert/www.aaa.com/fullchain.pem;
    ssl_certificate_key    /www/server/panel/vhost/cert/www.aaa.com/privkey.pem;
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers    ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    add_header Strict-Transport-Security "max-age=31536000";
    error_page 497  https://$host$request_uri;

    # websocket请求代理转发指定端口
    location  ~* /websocket{
        proxy_pass http://127.0.0.1:2020;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header X-Real-IP $remote_addr;
    }
}

这是前端代码
本人后端 前端代码是随便百度的

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <script type="text/javascript">
        function WebSocketTest() {
            // 打开一个 web socket
            var ws = new WebSocket("wss://www.aaa.com/websocket");
            ws.onopen = function () {
                // Web Socket 已连接上,使用 send() 方法发送数据
                ws.send("发送数据");
                alert("数据发送中...");
            };
            ws.onmessage = function (evt) {
                var received_msg = evt.data;
                alert("数据已接收...");
            };
            ws.onclose = function () {
                alert("连接已关闭...");
            }
            ws.onerror = function (ev) {
                console.log(this, ev);
            };
        }
    </script>
</head>
<body>
    <div id="sse">
        <a href="javascript:WebSocketTest()">运行 WebSocket</a>
    </div>
</body>
</html>

有没有大佬帮帮忙,万分感谢!

1550 5 0
5个回答

six

官方文档 https://www.workerman.net/doc/workerman/faq/secure-websocket-server.html
没看到nginx有http2的设置,http2去掉试下呗

oliusha

没有正则表达式,不用 ~*

PHP甩JAVA一条街
map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}
upstream websocket {
    server 你的ip:7272;  #这里可以是多个服务端IP(分多行),设置权重就可以实现负载均衡了
}
server
{
    listen 80;
    listen 443 ssl http2;
    server_name dp.xx.com;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/dp/dapai/public;

      #wss协议转发 小程序里面要访问的链接
    # 访问:wss://xxxx.com/wss
    location /wss {
        proxy_pass http://dp.xx.com:7272;#代理到上面的地址去,
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    ssl_certificate    /www/server/panel/vhost/cert/dp.xx.com/fullchain.pem;
    ssl_certificate_key    /www/server/panel/vhost/cert/dp.xx.com/privkey.pem;
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    add_header Strict-Transport-Security "max-age=31536000";
    error_page 497  https://$host$request_uri;

    #SSL-END

    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #ERROR-PAGE-END

    #PHP-INFO-START  PHP引用配置,可以注释或修改
    include enable-php-72.conf;
    #PHP-INFO-END

    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    include /www/server/panel/vhost/rewrite/dp.xx.com.conf;
    #REWRITE-END

    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }

    #一键申请SSL证书验证目录相关设置
    location ~ \.well-known{
        allow all;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log /dev/null;
        access_log /dev/null;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log /dev/null;
        access_log /dev/null; 
    }
    access_log  /www/wwwlogs/dp.xx.com.log;
    error_log  /www/wwwlogs/dp.xx.com.error.log;
}
  • PHP甩JAVA一条街 2023-08-29

    这是wss,nginx配置,JAVA不行了,市场份额越来越少了,我们好几个客户不打算用JAVA做项目,改用php+webman了

  • PHP甩JAVA一条街 2023-08-29

    关键JAVA费时间,工资要的老高,水平又一般,一个PHP程序员顶3个JAVA

  • Morning 2023-08-31

    谢谢大佬,搞好了,是我们运维的问题。。。

  • wangkaiw 2023-10-07

    请问一下,是运维的什么问题导致的?

oliusha

试试这样

location ~* ^/(.*)/websocket$
    {
        proxy_pass http://127.0.0.1:2020;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header X-Real-IP $remote_addr;
    }
  • Morning 2023-08-31

    谢谢大佬,搞好了,是我们运维的问题。。。

wangkaiw

请问楼主是怎么解决的,我遇到了同样的问题

  • sssd 2023-12-13

    大佬,你的问题解决了吗?请求指导一下

年代过于久远,无法发表回答
×
🔝