Apache 网站 + Nodejs 后端 + Websocket 服务器

Apache Website + Nodejs backend + Websocket server

我正在尝试让我的网站与 nodejs 后端和 websocket 服务器一起工作

我的网站完全是 https 我的节点后端在端口 8080 上,我的 websocket 服务器在 8080 上 我做了一个这样的虚拟主机

<VirtualHost *:8080>
  RewriteEngine On
  RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
  RewriteCond %{QUERY_STRING} transport=websocket    [NC]
  RewriteRule /(.*)           ws://localhost:8080/ [P,L]

  ProxyPass / http://localhost:8080/
  ProxyPassReverse / http://localhost:8080/
</VirtualHost>


<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
    DocumentRoot /var/www/example.com
    ServerName www.example.com
    ServerAlias example.com
    <Directory /var/www/example.com>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Allow from all
        Require all granted
    </Directory>
    <Directory /var/www/example.com/wp-content>
        Options -Indexes +FollowSymLinks +MultiViews
        Require all granted
    </Directory>

        SSLEngine on
        SSLCertificateFile    /etc/letsencrypt/live/example.com/fullchain.pem
        SSLCertificateKeyFile   /etc/letsencrypt/live/example.com/privkey.pem

<IfModule mod_headers.c>
        Header always set Strict-Transport-Security "max-age=15553000; includeSubDomains; preload"
</IfModule>

        ErrorLog /var/log/apache2/error.example.com.log
        CustomLog /var/log/apache2/access.example.com.log combined
</VirtualHost>

但是当我尝试继续 myip:8080 它不起作用,连接到我的 websocket 时也是如此 我做错了什么?

启用mod_proxy_wstunnel

那么您应该只能将您的位置转发到您的 websocket 服务器。

ProxyPass /wssurl/ ws://127.0.0.1:8080/

辛苦了。我想通了。我认为问题出在我安装网站时的防火墙上。

所以我必须代理通过我的安全连接上的所有内容。 mod_proxy_wstunnel 无法正常工作,因为当我尝试连接 wss:// 时,我不得不使用 rewriteEngine。

这是我最后的工作虚拟主机

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
    DocumentRoot /var/www/example.com
    ServerName www.example.com
    ServerAlias example.com
    <Directory /var/www/example.com>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Allow from all
        Require all granted
    </Directory>
    <Directory /var/www/example.com/wp-content>
        Options -Indexes +FollowSymLinks +MultiViews
        Require all granted
    </Directory>

        SSLEngine on
        SSLCertificateFile    /etc/letsencrypt/live/example.com/fullchain.pem
        SSLCertificateKeyFile   /etc/letsencrypt/live/example.com/privkey.pem

<IfModule mod_headers.c>
        Header always set Strict-Transport-Security "max-age=15553000; includeSubDomains; preload"
</IfModule>

        ErrorLog /var/log/apache2/error.example.com.log
        CustomLog /var/log/apache2/access.example.com.log combined

        ProxyRequests On
        ProxyPreserveHost On
        ProxyPass /api/test http://localhost:8080
        ProxyPassReverse /api/test http://localhost:8080

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

</VirtualHost>