【问题标题】:Config apache and nodejs socket.io配置 apache 和 nodejs socket.io
【发布时间】:2018-04-16 03:58:06
【问题描述】:

您好,我正在使用 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 连接网络套接字,它会说

加载失败 http://mywebsite.com/socket.io/?EIO=3&transport=polling&t=L-1lgZ1: 重定向自 'http://mywebsite.com/socket.io/?EIO=3&transport=polling&t=L-1lgZ1' 到 'https://mywebsie.com/socket.io/?EIO=3&transport=polling&t=L-1lgZ1' 已被 CORS 策略阻止:没有“访问控制允许来源” 请求的资源上存在标头。起源 'http://localhost:3001' 因此不允许访问。

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

加载失败 https://mywebsite.com/socket.io/?EIO=3&transport=polling&t=L-1lUP5:没有 请求中存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:3001” 使用权。响应的 HTTP 状态代码为 404。

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

我的配置有什么问题?

【问题讨论】:

  • 尝试添加ProxyPreserveHost On
  • 现在我有websocket.js:112 WebSocket connection to 'ws://mywebsite.com/socket.io/?EIO=3&amp;transport=websocket' failed: Error during WebSocket handshake: Unexpected response code: 301

标签: node.js apache sockets socket.io feathersjs


【解决方案1】:

问题解决了。 Apache 与 nginx 发生冲突,导致 301 错误。所以我使用 nginx 代理 websocket,它在子域上运行没有任何问题。

【讨论】:

    猜你喜欢
    • 2017-02-25
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多