![](/img/trans.png)
[英]Ratchet Websocket Apache2 SSL unable to make connection to PHP websocket
[英]php ratchet websocket SSL connect?
我有一個棘輪聊天服務器文件
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();
我使用 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.
}
我想要安全連接,所以我嘗試使用 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.
}
我的問題是如何使用 SSL 連接連接 websocket
任何的想法?
如果您使用的是 Apache Web 服務器(2.4 或更高版本),請在 httpd.conf 文件中啟用這些模塊:
將此設置添加到您的 httpd.conf 文件中
ProxyPass /wss2/ ws://ratchet.mydomain.org:8888/
當您需要 WSS 連接時,請在 JavaScript 調用中使用此 URL:
var ws = new WebSocket("wss://ratchet.mydomain.org/wss2/NNN");
在應用設置(telnet 主機名端口)之前,重新啟動 Apache Web 服務器並確保您的 Ratchet 工作線程(Web 套接字連接)處於打開狀態。
幾天前我正在尋找這個問題的答案,我在 Github Ratchet 問題中找到了這個: https : //github.com/ratchetphp/Ratchet/issues/489
我只為像我這樣需要快速說明如何實現 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
所以現在似乎有一種方法可以在不需要 HTTPS 代理的情況下使用 Ratchet 實現安全的 websocket 服務器。
這里有 SecureServer 類文檔: https : //github.com/reactphp/socket#secureserver
我在Chris Boden 的Ratchet 谷歌小組上找到了這個答案:
最好的解決方案是使用 Nginx 作為您的 Web 服務器。 讓 Nginx 在端口 80 上偵聽傳入連接並讓它處理您的 SSL。 Nginx 會將傳入連接轉發到您的常規網站的 PHP-FPM,如果它檢測到連接是 WebSocket 連接,則將其代理到您選擇的端口上正在運行的 Ratchet 應用程序。 然后您的 javascript 可以通過wss://mydomain.org連接
如果您使用的是 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/(.*) /$1 break;
proxy_redirect off;
# timeout extension, possibly keep this short if using a ping strategy
proxy_read_timeout 99999s;
}
這會將任何wss://yoursite.com/services/myservice
調用升級到運行在端口 1234 上的套接字。請確保您記住不要讓端口 1234 對外開放。
Apache 也為我工作,只需添加域 conf:
ProxyPass /wss/ wss://127.0.0.1:8888/
重新加載 apache,然后導入以在客戶端設置 wss 以包含/wss/位置
wss://127.0.0.1/wss/
如果您使用的是 Windows IIS,請確保已將其配置為 HTTPS(我使用的是自簽名證書),然后安裝反向代理:
URL 重寫: https : //www.iis.net/downloads/microsoft/url-rewrite和 ARR 3.0: https : //www.iis.net/downloads/microsoft/application-request-routing
創建用於 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>
並將“ http://127.0.0.1:8080 ”更改為您的websocket服務(我在WIN上使用Ratched for PHP)。
在 javascript 客戶端,使用安全的 websockets wss:// 協議,例如:
mysock = new WebSocket('wss://127.0.0.1/myproxyfolder');
...
它適用於 ubuntu 18.04。
var ws = new WebSocket('wss://domain.com/ws/');
通過在終端中運行以下命令啟用代理模塊。
sudo a2enmod 代理 proxy_balancer proxy_wstunnel proxy_http
在我的 Apache 虛擬主機配置文件(/etc/apache2/sites-available/000-default-le-ssl.conf)中添加了這些行
代理請求關閉
ProxyPass "/ws/" "ws://domain.com:5555/"
重啟apache服務。 並且 websocket 開始在 https 中工作。
我試圖為子域執行此操作。 例如:將 realtime.domain.org 從 apache 重定向到 localhost:8080。
這是它的工作原理。 您可以創建一個虛擬主機和代理傳遞。
<VirtualHost *:80>
ServerName realtime.domain.org
RewriteEngine On
RewriteCond %{HTTP:Connection} Upgrade [NC]
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteRule /(.*) ws://localhost:8080/$1 [P,L]
ProxyPreserveHost On
ProxyRequests off
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
因此,對 realtime.domain.org 的所有請求都可以重定向到端口 8080,您可以在該端口運行 WebSocket 處理程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.