PHP使用GatewayWorker 配置https问题、nginx配置同时支持WSS和HTTPS,客服系统websocket使用问题

前提:在阿里云上申请了ssl证书,然后把证书下载到服务器上,nginx配置(第3点)

1、客户端的请求链接要改成:wss://域名/wss  (http是用ws://ip:端口)

2、服务端的start_gateway.php中设置以下代码。官方文档:http://doc2.workerman.net/secure-websocket-server.html

// 证书最好是申请的证书
$context = array(
// 更多ssl选项请参考手册 http://php.net/manual/zh/context.ssl.php
'ssl' => array(
// 请使用绝对路径
'local_cert' => '磁盘路径/server.pem', // 也可以是crt文件
'local_pk' => '磁盘路径/server.key',
'verify_peer' => false,
// 'allow_self_signed' => true, //如果是自签名证书需要开启此选项
)
);
// websocket协议(端口任意,只要没有被其它程序占用就行)
$gateway = new Gateway("websocket://0.0.0.0:8282", $context);//这里官方文档是用443,但一般我们实际上服务器的443端口会用80端口来转多个项目,没有用,就可以用443
// 开启SSL,websocket+SSL 即wss
$gateway->transport = 'ssl';//(这里要注意,如果nginx中配置了wss代理如下,这里就不能开启,要注释掉,否则就开了两层ssl)

注意:

1、如果无法启动,则一般是443端口被占用,请改成其它端口。如果必须使用443端口请参考worekrman手册创建wss服务方法二部分。

2、wss端口只能通过wss协议访问,ws无法访问wss端口。

3、证书一般是与域名绑定的,所以测试的时候客户端请使用域名连接,不要使用ip去连。

4、如果出现无法访问的情况,请检查服务器防火墙。

5、此方法要求PHP版本>=5.6,因为微信小程序要求tls1.2,而PHP5.6以下版本不支持tls1.2。

3、nginx配置:nginx代理WSS只需添加红色部分即可,其它的都是监听80转443的HTTPS配置。

红色部分代码

location /wss {

            proxy_pass http://127.0.0.1:8282;

扫描二维码关注公众号,回复: 5726040 查看本文章

            proxy_http_version 1.1;

            proxy_set_header Upgrade $http_upgrade;

            proxy_set_header Connection "Upgrade";

            proxy_set_header X-Real-IP $remote_addr;

            proxy_read_timeout 3600s;

            #proxy_read_timeout 3600s这个是设置端口保持多久断开,除了设置这个,前端在超时时间内发心跳包,刷新再读时间,这样才能跟服务器保持长连接。

          }

红色部分workerman的文档有说明:http://doc.workerman.net/faq/secure-websocket-server.html

server {
    listen 80;
    server_name 域名;
    rewrite ^(.*)$ https://${server_name}$1 permanent;
}
server
    {
        listen 443;
        server_name 域名;
        root  项目目录;
        index index.php index.html index.htm;

        ssl_certificate   /usr/local/nginx/cert/customerService/cert-1542181289912_cm.9090club.com.crt;
        ssl_certificate_key  /usr/local/nginx/cert/customerService/cert-1542181289912_cm.9090club.com.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

        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;
          }

        location / {
                    if (!-e $request_filename){
                        rewrite ^(.*)$ /index.php?s=$1 last;
                        #rewrite ^/index.php/(.*)$ /index.php?s=$1 last;
                        break;
                    }
                }

        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        #include proxy-pass-php.conf;

        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }

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

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }
        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ \.php$ {
            #root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        access_log  这里填access.log路径;
        error_log  这里填error_log.log路径;
    }

猜你喜欢

转载自blog.csdn.net/itbird58/article/details/84108095
今日推荐