nginx 反向代理 workerman的 websocket 一直连接不上

zxd1021


服务端代码是这么写的

<?php

include __DIR__ . '/workerman/Autoloader.php';
use Workerman\Worker;
// 这里设置的是websocket协议(端口任意,但是需要保证没被其它程序占用)
$worker = new Worker('websocket://0.0.0.0:1010');
// 设置transport开启ssl,websocket+ssl即wss
//$worker->transport = 'ssl';
$worker->onMessage = function($connection, $data)
{
    $connection->send("hello");
};

Worker::runAll();

下面是 nginx的配置文件

server {
    listen 80;
    server_name xxxxxxxxxxxxxx;
    rewrite ^(.*)$ https://$host$1 permanent;
}
server {
        listen  443 ssl;
        server_name  xxxxxxxxxxxxxx;
        index index.html index.htm index.php;

        ssl on;
        ssl_certificate  /data/home/server/nginx-1.7/conf/ssl/xxxxxxxxxxxxxx/xxxxxxxxxxxxxx.pem;
        ssl_certificate_key /data/home/server/nginx-1.7/conf/ssl/xxxxxxxxxxxxxx/xxxxxxxxxxxxxx.key;
        ssl_session_timeout 5m;
        ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2;
        ssl_session_cache shared:SSL:50m;
        ssl_prefer_server_ciphers on;
        location / {
            root /data/home/www/xxxxxxxxxxxxxx/public;
            index  index.php index.html index.htm;
            # Nginx环境配置,隐藏ThinkPHP public index.php入口
            #try_files $uri $uri/ /index.php?s=$uri&$args;
            try_files $uri $uri/ /index.php?s=$args;
            if (!-e $request_filename) {
                    rewrite  ^(.*)$  /index.php?s=/$1  last;
                    break;
            }
            }
            #error_page  404              /404.html;
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
              location /wss
              {
                      proxy_pass http://127.0.0.1:8686;
                      proxy_set_header X-Real-IP $remote_addr;
                      proxy_set_header Host $host;
                      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                      proxy_http_version 1.1;
                      proxy_set_header Upgrade $http_upgrade;
                      proxy_set_header Connection "upgrade";
                      proxy_redirect off;
              }
            location ~ \.php { #去掉$
                root /data/home/www/xxxxxxxxxxxxxx/public;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
            fastcgi_split_path_info ^(.+\.php)(.*)$;     #增加这一句
            fastcgi_param PATH_INFO $fastcgi_path_info;    #增加这一句
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
            }
#   include /alidata/server/nginx/conf/rewrite/phpmyadmin.conf;
    #access_log  /alidata/log/nginx/access/phpmyadmin.log;
}

这个启动完的显示

----------------------- WORKERMAN -----------------------------
Workerman version:3.5.14          PHP version:7.0.0
------------------------ WORKERS -------------------------------
proto    user          worker        listen                      processes status
tcp      root          none          websocket://127.0.0.1:8686   4          
----------------------------------------------------------------
Press Ctrl+C to stop. Start success.

网页代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv = "X-UA-Compatible" content = "IE=edge,chrome=1" />
    <title>撒的发撒的发</title>
</head>
<body class="gray-bg">
<script language="javascript">
    ws = new WebSocket("wss://xxxxxxxxxx/wss");
    ws.onopen = function() {
        alert("连接成功");
        ws.send('tom');
        alert("给服务端发送一个字符串:tom");
    };
    ws.onmessage = function(e) {
        alert("收到服务端的消息:" + e.data);
    };
    ws.onerror = function(e) {
        console.log(e);
    };
</script>
</body>
</html>

一直连接超时504

13633 11 0
11个回答

zxd1021

WebSocket connection to 'wss://xxxxxxx/wss' failed: Error during WebSocket handshake: Unexpected response code: 504

浏览器抛出的是这个错误提示,我排查了一下有可能是通讯协议的问题,但是我实在是不知道怎么改

  • 暂无评论
zxd1021

上面写错的端口号也调整成一致了,并没有什么卵用

  • 暂无评论
walkor 打赏

按照手册把nginx多余的配置去掉试下,没问题了再把nginx一点一点加回来看下是哪里问题

  • zxd1021 2018-09-05

    大神我用的是 nginx1.7版本 跟手册一样的配置是报404的,然后我就 加 了server_name 之后变成504错误 脑壳疼

  • zxd1021 2018-09-05

    大神我现在在社区里面找配置文件照着改,然而一直是301 502 504 这三个直接徘徊

  • zxd1021 2018-09-05

    我现在的有一些疑问就是 这个对nginx的版本有要求吗,然后还有就是支持跨域吗,

  • walkor 2018-09-05

    根据手册,nginx 版本不能小于1.3

  • zxd1021 2018-09-05

    那我满足了 我的版本是1.7的 其次跨域的那个现在不用考虑 最后各种姿势的配置文件我也尝试过了但就是gg 各种504 是我脸黑吗 还是我姿势不对=.=

  • walkor 2018-09-05

    把你现在的配置贴出来

  • zxd1021 2018-09-05

    好的 稍等

  • zxd1021 2018-09-05

    发出来了

zxd1021
server {
    listen 443 ssl;
    server_name xxxxxx.com;
    root /data/home/www/songshihuishou/public;

    ssl on;
    ssl_certificate  /data/home/server/nginx-1.7/conf/ssl/1537639025967/1537639025967.pem;
    ssl_certificate_key /data/home/server/nginx-1.7/conf/ssl/1537639025967/1537639025967.key;
    ssl_session_timeout 5m;
    ssl_session_cache shared:SSL:50m;
    ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

    location /wss
    {
        proxy_pass http://127.0.0.1:8282;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header X-Real-IP $remote_addr;
    }
}
  • 暂无评论
walkor 打赏

workerman 的
php start.php status 贴下

zxd1021
<?php
include __DIR__ . '/workerman/Autoloader.php';
use Workerman\Worker;
if(strpos(strtolower(PHP_OS), 'win') === 0)
{
    exit("start.php not support windows, please use start_for_win.bat\n");
}

// 标记是全局启动
define('GLOBAL_START', 1);
// 这里设置的是websocket协议(端口任意,但是需要保证没被其它程序占用)
$ws_worker = new Worker('websocket://0.0.0.0:8282');
// 设置transport开启ssl,websocket+ssl即wss
//$ws_worker->transport = 'ssl';
// 启动4个进程对外提供服务
$ws_worker->count = 4;
// 当收到客户端发来的数据后返回hello $data给客户端
$ws_worker->onMessage = function($connection, $data){
    // 向客户端发送hello $data
    $connection->send('hello ');
};

Worker::runAll();

这个是后端的代码

  • 暂无评论
zxd1021
Workerman status 
----------------------------------------------GLOBAL STATUS----------------------------------------------------
Workerman version:3.5.14          PHP version:7.0.0
start time:2018-09-05 09:58:08   run 0 days 0 hours   
load average: 0, 0, 0            event-loop:\Workerman\Events\Select
1 workers       4 processes
worker_name  exit_status      exit_count
none         0                0
----------------------------------------------PROCESS STATUS---------------------------------------------------
pid memory  listening                worker_name  connections send_fail timers  total_request qps    status
19991   2M      websocket://0.0.0.0:8282 none         0           0         0       0             0      
19992   2M      websocket://0.0.0.0:8282 none         0           0         0       0             0      
19993   2M      websocket://0.0.0.0:8282 none         0           0         0       0             0      
19994   2M      websocket://0.0.0.0:8282 none         0           0         0       0             0      
----------------------------------------------PROCESS STATUS---------------------------------------------------
Summary 8M      -                        -            0           0         0       0             0       
  • 暂无评论
walkor 打赏

客户端连接代码发下,我测试下,不要隐藏域名

walkor 打赏

看下你的服务器

server {
listen 443 ssl;
server_name xxxxxx.com;

xxxxxx.com是什么

  • 暂无评论
walkor 打赏

还有看下是不是用了cdn

  • zxd1021 2018-09-05

    好像是的,用了cdn 中间隔了个服务器

  • zxd1021 2018-09-05

    我朋友给我解析的 打开直接就是https 应该是用了cdn

  • zxd1021 2018-09-05

    感谢大神,真的是用了cdn, 等解析生效了我在试试 三克油

  • zxd1021 2018-09-05

    虽然被cdn卡了一天,但是解决了 很美滋滋 谢谢大神

  • walkor 2018-09-05

    不客气

且听风吟

多谢多谢,我这边也碰到类似的问题,我的域名解析也用了cdn,请问你是怎么解决的?

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