簡體   English   中英

代理 websocket wss:// 到 ws:// apache

[英]Proxy websocket wss:// to ws:// apache

我搜索了很多,但我無法將我的 websocket 連接到 wss:// ,我發現有一種方法可以代理 wss://domain.com:9090 並且 apache 在其上應用代理並將請求重定向到正常 ws:/ /domain.com:9090 服務器正在運行

ProxyPass /websocket ws://domain.com:9090
ProxyPassReverse /websocket ws://domain.com:9090

apache 配置中的此代碼將從以 /websocket 結尾的任何地址發送請求到 ws://domain.com:9090 例如:ws://websocket 將是 ws://domain.com:9090

我想為 wss:// 也這樣做 wss://websocket 必須指向 ws://domain.com:9090

它不起作用,我在瀏覽器控制台中收到此錯誤:

failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

這里有什么錯誤嗎? 謝謝你。

我工作了 24 小時才找到這個並搜索了很多論壇,但沒有人寫關於成功的文章。 這是我的服務器配置:

CentOS 6.7 版,Apache 4.2.18

這是我最后所做的:首先我發現必須在 apache 配置文件中啟用 modules/mod_proxy_wstunnel.so,但是我的 apache 沒有那個模塊,經過大量搜索我發現該模塊在 apache 2.4.5 中可用后來。

https://httpd.apache.org/docs/2.4/mod/mod_proxy_wstunnel.html

我下載了https://archive.apache.org/dist/httpd/httpd-2.4.18.tar.gz提取了 httpd-2.4.18\\modules\\proxy\\mod_proxy_wstunnel.c 並上傳到我的服務器根目錄然后從終端可以編譯再次與這些共同點:

chmod 755 mod_proxy_wstunnel.c #set permission
pxs -i -a -c mod_proxy_tunnel.c #compile module

pxs 命令確實編譯了模塊並寫入了 apache 配置文件以加載它

LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so

之后,我將這些行添加到 apache 配置文件的末尾:

RewriteEngine on
ProxyRequests Off
ProxyPreserveHost on
ProxyPass /myws ws://mysite.com:8091
ProxyPassReverse /myws ws://mysite.com:8091

現在:它起作用了! 在客戶端 js 中,您可以像這樣設置 ws url:

var protocol = 'ws://'; 
if (window.location.protocol === 'https:') {
            protocol = 'wss://';
   }

 var wsUri =protocol+ "mysite.com/myws";  

 var ws = new WebSocket(wsUri);

它會將請求轉發到 ws://mysite.com:8091 與加載 https 或 http 的頁面無關,它將所有以 /myws 結尾的請求定向到 ws://mysite.com:8091

您需要啟用一些 Apache2 模塊:

$ a2enmod proxy proxy_wstunnel proxy_http rewrite

然后你可以使用這個配置來解決你的問題。

    ProxyRequests off
    ProxyVia on      
    RewriteEngine On

    RewriteEngine On
    RewriteCond %{HTTP:Connection} Upgrade [NC]
    RewriteCond %{HTTP:Upgrade} websocket [NC]
    RewriteRule /(.*) ws://example.com:9090/$1 [P,L]

    ProxyPass               /websocket http://example.com:9090/websocket
    ProxyPassReverse        /websocket http://example.com:9090/websocket

Apache2 自動升級到帶有 ws:// 的 websocket 連接,您不需要手動設置 ws://。 我嘗試了幾十種配置,這是唯一對我有用的配置。

wss 需要在 apache conf LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so 的 httpd.conf 中的模塊取消注釋行

我試圖解決的問題與此類似。 我有一個在 CentOs 7 上的 Apache 2.4 下運行的反向代理,它必須同時處理 https 和 wss 請求。

在反向代理的背后,我的應用服務器運行在內部網絡上。 /etc/httpd/httpd.conf配置文件中的虛擬主機配置如下:

<VirtualHost *:443>
   ServerName example.com
   RewriteCond %(HTTP:Upgrade) websocket [NC]   # Required to handle the websocket connection
   RewriteCond %(HTTP:Connection) upgrade [NC]
   RewriteRule /(.*) ws://192.160.0.1/$1 [P,L]

  SSLEngine on # SSL Certificates handling
  SSLCertificateFile ssl/cert.pem # Public Certificate
  SSLCertificateKeyFile ssl/key.pem # Private certificate
  SSLCertificateChainFile ssl/ca.pem # CA or chain certificate

 ProxyPreserveHost On
 ProxyPass /websocket ws://192.168.0.1 # First you need to write the specific rules
 ProxyPassReverse /websocket ws://102.168.0.1
 ProxyPass / http://192.168.0.1 # Then the generic rules for the proxy.
 ProxyPassReverse / http://192.168.0.1
 </VirtualHost>

在您的情況下,您必須替換 ServerName、SSL 證書位置和代理的目的地。

您的 ProxyPass 配置路徑中缺少 /websocket 路徑。

使用:

ProxyPass /websocket ws://example.com:9090/websocket
ProxyPassReverse /websocket ws://example.com:9090/websocket

附加信息:與其他人提到的一樣,您必須取消注釋該行:

LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so

如果您還使用相對路徑為“/”(直接轉發所有內容)的http ProxyPass,則必須先配置特定的“/websocket”路徑配置,否則“/”會先抓取。

ProxyPass /websocket ws://example.com:9090/websocket
ProxyPassReverse /websocket ws://example.com:9090/websocket

ProxyPass balancer://ac-cluster/
ProxyPassReverse / http://example.com:9090

我是為 aria 2 做的。我只是啟用了一些模塊並在配置中添加了一行。 (環境:debian buster/apache 2.4)。 啟用模式:

sudo a2enmod proxy proxy_balancer proxy_wstunnel proxy_http

並將這一行添加到虛擬主機指令內的 ssl 站點配置文件:

ProxyPass /jsonrpc ws://127.0.0.1:6888/jsonrpc

我想分享這個,以防它幫助其他人避免浪費時間和精力。

在研究了一切之后我放棄了。 我准備開始遵循不同代理模塊的代碼,是的,我知道,蜘蛛網......,但我很絕望。 作為最后一個資源,我安裝了wireshark 以准確跟蹤我的網絡中發生的情況。 安裝wireshark后,說明要求我通過斷電/開機循環重新啟動我的服務器。 所以,我做到了。 當我開始跟蹤它時,令我完全驚訝的是,服務器完美地將 wss 請求代理到 ws,沒有問題! 所以我有正確的設置開始,但是在 Ubuntu 20.4 / Apache 2.4.41 / node 14.17.2 中出現了一些問題,需要完全重新啟動服務器運行的機器。 瘋了! 但就是這樣...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM