配置 apache 和 nodejs socket.io

Config apache and nodejs socket.io

您好,我正在使用 apache 2.2 和 feathersjs 开发网站。 在这个网站上,我们有 DV SSL,所以我们无法使用 https 访问子域。

我的节点应用程序在 3030 端口上 运行。所以我可以使用这个地址访问 feathersjs 应用程序:

http://mywebsite.com:3030

https://mywebsite.com:3030 不工作。

我需要使用像 https://mywebsite.com/socket/ 这样的位置来连接到 feathersjs websocket(猜测 ws://localhost:3030)

这是客户端代码:

const host = 'https://mywebsite.com/socket';
const socket = io(host,{
            transports: ['websocket'],
            forceNew: true
        });

我需要 httpd 配置才能通过 https 连接 ws。

post_virtualhost_global.conf

<VirtualHost 185.173.106.42:80>
  ServerName mywebsite.com

    RewriteEngine on
    RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
    RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
    RewriteRule .* ws://localhost:3030%{REQUEST_URI} [P]

    ProxyPass /socket http://localhost:3030/
    ProxyPassReverse /socket http://localhost:3030/

    ProxyRequests off

    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

</VirtualHost>

<VirtualHost 185.173.106.42:443>
  ServerName freevery.com

    RewriteEngine on
    RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
    RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
    RewriteRule .* ws://localhost:3030%{REQUEST_URI} [P]

    ProxyPass /socket http://localhost:3030/
    ProxyPassReverse /socket http://localhost:3030/

    ProxyRequests off

    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    <Location /feathers>
        ProxyPass http://localhost:3030/
        ProxyPassReverse http://localhost:3030/
    </Location>

</VirtualHost>

如果我尝试使用 http://mywebsite.com/socket 连接网络套接字,它会显示

Failed to load http://mywebsite.com/socket.io/?EIO=3&transport=polling&t=L-1lgZ1: Redirect from 'http://mywebsite.com/socket.io/?EIO=3&transport=polling&t=L-1lgZ1' to 'https://mywebsie.com/socket.io/?EIO=3&transport=polling&t=L-1lgZ1' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3001' is therefore not allowed access.

如果我尝试使用 https://mywebsite.com/socket 连接网络套接字,它会说

Failed to load https://mywebsite.com/socket.io/?EIO=3&transport=polling&t=L-1lUP5: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3001' is therefore not allowed access. The response had HTTP status code 404.

我将 Header set Access-Control-Allow-Origin "*" 添加到我的 .htaccess 文件,但问题仍然存在。

我的配置有什么问题?

问题已解决。 Apache 与 nginx 发生冲突,并出现 301 错误。所以我使用 nginx 来代理 websocket,它在子域上工作没有任何问题。