首页 > 解决方案 > Apache2 https:为实时流启用 websockets

问题描述

我正在尝试在 ubuntu os 的 apache2 https 服务器下安装我的 angular 应用程序。

我按照此链接https://www.digitalocean.com/community/tutorials/how-to-create-a-self-signed-ssl-certificate-for-apache-in-ubuntu-18-04在 apache2 下启用 https . 因此 apache2 https 站点在 /etc/apache2/sites-available/default-ssl.conf 中配置,如下所示:

<IfModule mod_ssl.c>
    <VirtualHost *:443>
            ServerAdmin webmaster@localhost
            ServerName https://my_ip/
            DocumentRoot /var/www/html

            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined

            SSLEngine on

            SSLCertificateFile      /etc/ssl/certs/ssl-cert-snakeoil.pem
            SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

            <FilesMatch "\.(cgi|shtml|phtml|php)$">
                    SSLOptions +StdEnvVars
            </FilesMatch>
            <Directory /usr/lib/cgi-bin>
                    SSLOptions +StdEnvVars
            </Directory>
    </VirtualHost>
</IfModule>

重新启动 apache2 后,我能够通过 https 访问我的应用程序并轻松导航到所有页面,但唯一不能正常工作的是 rtsp 直播。

我的应用程序类似于我在这个问题中描述的内容:无法使用 Angular 显示实时摄像机 RTSP 流

因此,通过 rtsp 代理使用 JSMPEG 库在我的 Angular 应用程序中显示实时流:

new JSMpeg.Player('ws://my_ip:9999', {
    canvas: this.streamingcanvas.nativeElement, autoplay: true, audio: false, loop: true
      })

如果使用 npm start 执行它,我的 Angular 应用程序可以正常工作。但是在https服务器下流不显示!!

经过思考,我猜可能有一些与 websockets 相关的东西应该在我的 apache2 服务器中配置。搜索后我找到了这个链接:https ://www.serverlab.ca/tutorials/linux/web-servers-linux/how-to-reverse-proxy-websockets-with-apache-2-4/

所以我用 a2enmod 命令在 apache2 中启用了 ws 并添加了这些行:

RewriteEngine on
RewriteCond ${HTTP:Upgrade} websocket [NC]
RewriteCond ${HTTP:Connection} upgrade [NC]
RewriteRule .* "wss://my_ip:9999/$1" [P,L]

ProxyPass / https://my_ip:9999/
ProxyPassReverse / https://my_ip:9999/
ProxyRequests off

到 VirtualHost 标签下的 /etc/apache2/sites-available/default-ssl.conf 文件。但这并不能解决问题。

有什么建议可以解决这个问题吗?

标签: angularapachehttpswebsocketapache2-module

解决方案


推荐阅读