【问题标题】:Apache Website + Nodejs backend + Websocket serverApache 网站 + Nodejs 后端 + Websocket 服务器
【发布时间】:2021-10-15 15:23:21
【问题描述】:

我正在尝试让我的网站与 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/$1 [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 我做错了什么?

【问题讨论】:

    标签: node.js apache websocket backend


    【解决方案1】:

    启用mod_proxy_wstunnel

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

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

    【讨论】:

      【解决方案2】:

      经过大量工作后确定。我想通了。我认为问题出在我安装网站时的防火墙。

      所以我必须代理通过我的安全连接上的所有内容。 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/$1" [P,L]
      
      </VirtualHost>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-17
        • 1970-01-01
        • 2022-01-01
        • 1970-01-01
        • 2014-09-08
        • 2020-11-26
        • 1970-01-01
        相关资源
        最近更新 更多