0

我正在使用网络套接字。在 apache 中,我可以通过在配置文件中执行以下设置来从 https 重定向到 http:

      <VirtualHost *:443>
      # Common SSL Config
      ServerName ec2-13-52-248-221.us-west-1.compute.amazonaws.com
      SSLEngine on
      # wss redirects to working ws protocol

            SSLCertificateFile      /etc/ssl/certs/apache-selfsigned.crt
            SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
            ProxyPass "/"  "http://ec2-13-52-248-221.us-west-1.compute.amazonaws.com" retry=0 keepalive=On

      </VirtualHost>

但相同的配置(如上)我更改如下

     <VirtualHost *:443>
      # Common SSL Config
      ServerName ec2-13-52-248-221.us-west-1.compute.amazonaws.com
      SSLEngine on
      # wss redirects to working ws protocol

            SSLCertificateFile      /etc/ssl/certs/apache-selfsigned.crt
            SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
            ProxyPass "/wss"  "ws://ec2-13-52-248-221.us-west-1.compute.amazonaws.com:8080" retry=0 keepalive=On

      </VirtualHost>

我无法从 wss 重定向到 ws

在 Wss 的情况下,我在发送请求时使用以下网址:

    wss://ec2-13-52-248-221.us-west-1.compute.amazonaws.com:8080/wss
4

1 回答 1

1

经过24小时的努力终于我得到了解决方案:这个配置对我有用

首先通过在 Windows 的 httpd.conf 文件中启用此模块来加载以下模块

proxy_wstunnel_module modules/mod_proxy_wstunnel.so

在 linux 中,您可以通过以下命令启用此模块

sudo a2enmod proxy proxy_balancer proxy_wstunnel proxy_http

然后只需将以下行添加到 apache.conf 文件或 httpd-vhosts.conf 文件

RewriteEngine on
ProxyRequests Off
ProxyPreserveHost on
ProxyPass /ws ws://192.168.43.31:8080 retry=0 keepalive=On 
ProxyPassReverse /ws  ws://192.168.43.31:8080 retry=0

在 Javascript 文件中的更改是这样的:

if(location.protocol === 'http:'){
  var conn = new WebSocket('ws://192.168.43.31/ws');
 }
else{
  var conn = new WebSocket('wss://192.168.43.31/ws');
}

因此,从此 apache 会将所有具有 ws 的请求重定向回 8080 端口

于 2020-04-26T09:39:19.850 回答