php php棘轮websocket SSL连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16979793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:00:48  来源:igfitidea点击:

php ratchet websocket SSL connect?

phpsslwebsocketratchet

提问by vietnguyen09

I have a ratchet chat server file

我有一个棘轮聊天服务器文件

use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
use MyAppChat\Chat;
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
    new WsServer(
        new Chat()
    )
  , 26666
);
$server->run();

I using Websocket to connect with wsand it works fine

我使用 Websocket 连接,ws它工作正常

if ("WebSocket" in window) {
    var ws = new WebSocket("ws://ratchet.mydomain.org:8888");
    ws.onopen = function() {
        // Web Socket is connected. You can send data by send() method.
        ws.send("message to send");
    };
    ws.onmessage = function (evt) { 
        var received_msg = evt.data;
    };
    ws.onclose = function() { 
        // websocket is closed. 
    };
} else {
  // the browser doesn't support WebSocket.
}

I want secure connection, so I try to connect with SSL but is not work.

我想要安全连接,所以我尝试使用 SSL 进行连接,但不起作用。

if ("WebSocket" in window) {
    var ws = new WebSocket("wss://ratchet.mydomain.org:8888");
    ws.onopen = function() {
        // Web Socket is connected. You can send data by send() method.
        ws.send("message to send");
    };
    ws.onmessage = function (evt) { 
        var received_msg = evt.data;
    };
    ws.onclose = function() { 
        // websocket is closed. 
    };
} else {
  // the browser doesn't support WebSocket.
}

My question is how to connect websocket with SSL connection

我的问题是如何使用 SSL 连接连接 websocket

Any idea?

任何的想法?

回答by webcoder

If you are using Apache web server (2.4 or above), enable these modules in httpd.conf file :

如果您使用的是 Apache Web 服务器(2.4 或更高版本),请在 httpd.conf 文件中启用这些模块:

  1. mod_proxy.so
  2. mod_proxy_wstunnel.so
  1. mod_proxy.so
  2. mod_proxy_wstunnel.so

Add this setting to your httpd.conf file

将此设置添加到您的 httpd.conf 文件中

ProxyPass /wss2/ ws://ratchet.mydomain.org:8888/

Use this URL in your JavaSscript call when you want a WSS connection:

当您需要 WSS 连接时,请在您的 JavaScript 调用中使用此 URL:

var ws = new WebSocket("wss://ratchet.mydomain.org/wss2/NNN");

Restart Apache web server and make sure that your Ratchet worker (web socket connection) is open before applying the settings (telnet hostname port).

在应用设置(telnet 主机名端口)之前,重新启动 Apache Web 服务器并确保您的 Ratchet 工作线程(Web 套接字连接)处于打开状态。

回答by mattexx

The problem is that React (which Ratchet is built on) does not support direct SSL connections. See this issue.

问题在于 React(基于 Ratchet 构建)不支持直接 SSL 连接。看到这个问题

There is a simple workaround. Use stunnelwith a config like:

有一个简单的解决方法。将stunnel与如下配置一起使用:

[websockets]
accept = 8443
connect = 8888

Stunnel will handle SSL traffic on port 8443 and port them to your websocket server.

Stunnel 将处理端口 8443 上的 SSL 流量并将它们移植到您的 websocket 服务器。

回答by Jordi

A few days ago I was looking for the answer of this question and I found this in the Github Ratchet issues: https://github.com/ratchetphp/Ratchet/issues/489

几天前我正在寻找这个问题的答案,我在 Github Ratchet 问题中找到了这个:https: //github.com/ratchetphp/Ratchet/issues/489

The last answer, answered by heidji, says this:

heidji回答的最后一个答案是这样说的:

I only added this comment for newbies like me who need a quick instruction how to implement SSL: Via the ReactPHP docs you only need to construct the SecureServer mentioned in such manner:
$webSock = new React\Socket\Server('0.0.0.0:8443', $loop);
$webSock = new React\Socket\SecureServer($webSock, $loop, ['local_cert' => '/etc/ssl/key.pem', 'allow_self_signed' => true, 'verify_peer' => false]);
and then inject into the IoServer as mentioned by cboden above

我只为像我这样需要快速说明如何实现 SSL 的新手添加了此评论:通过 ReactPHP 文档,您只需要构建以这种方式提到的 SecureServer:
$webSock = new React\Socket\Server('0.0.0.0:8443', $loop);
$webSock = new React\Socket\SecureServer($webSock, $loop, ['local_cert' => '/etc/ssl/key.pem', 'allow_self_signed' => true, 'verify_peer' => false]);
然后注入到上面 cboden 提到的 IoServer

So it seems that now there is a way to implement a secure websocket server with Ratchet without needing an HTTPS proxy.

所以现在似乎有一种方法可以在不需要 HTTPS 代理的情况下使用 Ratchet 实现安全的 websocket 服务器。

Here you have the SecureServer class documentation: https://github.com/reactphp/socket#secureserver

这里有 SecureServer 类文档:https: //github.com/reactphp/socket#secureserver

回答by Songo

I found this answer on Ratchet's google groupby Chris Boden:

我发现这个答案棘轮的谷歌集团克里斯·博登

The best solution would be to use Nginx as your web server. Have Nginx listen on port 80 for incoming connections and have it handle your SSL. Nginx will forward incoming connections to PHP-FPM for your regular website and if it detects a connection is a WebSocket connection have it proxy to your running Ratchet application on a port of your choice. Your javascript could then connect via wss://mydomain.org

最好的解决方案是使用 Nginx 作为您的 Web 服务器。让 Nginx 在端口 80 上侦听传入连接并让它处理您的 SSL。Nginx 会将传入连接转发到您的常规网站的 PHP-FPM,如果它检测到连接是 WebSocket 连接,则将其代理到您选择的端口上正在运行的 Ratchet 应用程序。然后您的 javascript 可以通过wss://mydomain.org连接

This is an alternative way to using stunnelif your application is going to be served using nginx.

如果您的应用程序将使用nginx提供服务,这是使用stunnel的另一种方法。

回答by RaisinBranCrunch

If you're using Nginx, just write this in your SSL server block:

如果您使用的是 Nginx,只需在您的 SSL 服务器块中写入:

location /services/myservice {
    # switch off logging
    access_log off;

    # redirect all HTTP traffic to localhost
    proxy_pass http://localhost:1234;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    # WebSocket support (nginx 1.4)
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    # Path rewriting
    rewrite /services/myservice/(.*) / break;
    proxy_redirect off;

    # timeout extension, possibly keep this short if using a ping strategy
    proxy_read_timeout 99999s;
}

This will upgrade any wss://yoursite.com/services/myservicecall to a socket running on port 1234. Just make sure you remember not to leave port 1234 open to the world.

这将升级wss://yoursite.com/services/myservice对在端口 1234 上运行的套接字的任何调用。请确保记住不要让端口 1234 对外开放。

回答by rukavina

Apache also worked for me, just add in domain conf:

Apache 也为我工作,只需添加域 conf:

ProxyPass /wss/ wss://127.0.0.1:8888/

Reload apache and then it's import to set wss in client side to include /wss/location

重新加载 apache,然后导入以在客户端设置 wss 以包含/wss/位置

wss://127.0.0.1/wss/

回答by W.g.

If you are using Windows IIS, make sure that you have configured it for HTTPS (I'm using self signed certificate), then install reverse proxy:

如果您使用的是 Windows IIS,请确保您已将其配置为 HTTPS(我使用的是自签名证书),然后安装反向代理:

URL rewrite: https://www.iis.net/downloads/microsoft/url-rewriteand ARR 3.0: https://www.iis.net/downloads/microsoft/application-request-routing

URL 重写:https: //www.iis.net/downloads/microsoft/url-rewrite和 ARR 3.0:https: //www.iis.net/downloads/microsoft/application-request-routing

You also need to enable websockets support in IIS:enter image description here

您还需要在 IIS 中启用 websockets 支持:在此处输入图片说明

create folder (e.g. myproxyfolder) for URL rewrite, on this folder create web.config file with content:

创建用于 URL 重写的文件夹(例如 myproxyfolder),在此文件夹上创建包含内容的 web.config 文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="WebSocketProxy" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://127.0.0.1:8080" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

and change "http://127.0.0.1:8080" to your websocket service (I'm using Ratched for PHP on WIN).

并将“ http://127.0.0.1:8080”更改为您的websocket服务(我在WIN上使用Ratched for PHP)。

On client side in javascript, use secure websockets wss:// protocol, like:

在 javascript 客户端,使用安全的 websockets wss:// 协议,例如:

    mysock = new WebSocket('wss://127.0.0.1/myproxyfolder');
...

回答by Rakesh Kumar Srivastava

It is working for me for ubuntu 18.04.

它适用于 ubuntu 18.04。

var ws = new WebSocket('wss://domain.com/ws/');

var ws = new WebSocket('wss://domain.com/ws/');

Enabled proxy modules by running the following command in terminal.

通过在终端中运行以下命令启用代理模块。

sudo a2enmod proxy proxy_balancer proxy_wstunnel proxy_http

sudo a2enmod 代理 proxy_balancer proxy_wstunnel proxy_http

Added these lines in my Apache virtualhost config file(/etc/apache2/sites-available/000-default-le-ssl.conf)

在我的 Apache 虚拟主机配置文件(/etc/apache2/sites-available/000-default-le-ssl.conf)中添加了这些行

ProxyRequests Off

代理请求关闭

ProxyPass "/ws/" "ws://domain.com:5555/"

ProxyPass "/ws/" "ws://domain.com:5555/"

Restarted apache service. And the websocket started working in https.

重启apache服务。并且 websocket 开始在 https 中工作。

回答by Supun Kavinda

I was trying to do this for a subdomain. Ex: Redirect realtime.domain.org to localhost:8080 from apache.

我试图为子域执行此操作。例如:将 realtime.domain.org 从 apache 重定向到 localhost:8080。

Here's how it worked. You can create a virtual host and proxy pass that.

这是它的工作原理。您可以创建一个虚拟主机和代理传递。

<VirtualHost *:80>
    ServerName realtime.domain.org

    RewriteEngine On
    RewriteCond %{HTTP:Connection} Upgrade [NC]
    RewriteCond %{HTTP:Upgrade} websocket [NC]
    RewriteRule /(.*) ws://localhost:8080/ [P,L]

    ProxyPreserveHost On
    ProxyRequests off
    ProxyPass / http://localhost:8080/
    ProxyPassReverse / http://localhost:8080/
</VirtualHost>

So, all the requests to realtime.domain.org can be redirected to port 8080, where you can run the WebSocket handler.

因此,对 realtime.domain.org 的所有请求都可以重定向到端口 8080,您可以在该端口运行 WebSocket 处理程序。