简体   繁体   English

Apache + Node.JS + Socket.IO + CloudFlare // SSL错误

[英]Apache + Node.JS + Socket.IO + CloudFlare // SSL Error

I'm trying to run Apache with Node.JS + Socket.IO using Cloudflare for CDN / protection, but something is going wrong. 我正在尝试使用Cloudflare为CDN /保护使用Node.JS + Socket.IO运行Apache,但是出了点问题。

I've tryied a lot of ways to Apache handle Socket.IO connection (that is not through SSL) including ProxyPass 我尝试了很多方法来处理Apache的Socket.IO连接(不是通过SSL),包括ProxyPass

<VirtualHost *:80>
    ServerName      play.example.me
    ServerAlias     example.me
    DocumentRoot    /home/web/
    AccessFileName  .htaccess

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

    ProxyRequests off

    <Location /socket.io>
        ProxyPreserveHost On
        ProxyPass           ws://localhost:8080/socket.io/
        ProxyPassReverse    ws://localhost:8080/socket.io/
    </Location>
</VirtualHost> 

This gives 520 HTTP Error, that is a Cloudflare general error. 这给出520 HTTP错误,这是Cloudflare的一般错误。

I've tryied to use an SSL cert from Let's Encrypt on Node.JS + Socket.IO, but it gives ERR_SSL_PROTOCOL_ERROR. 我尝试使用Node.JS + Socket.IO上的“让我们加密”中的SSL证书,但是它提供了ERR_SSL_PROTOCOL_ERROR。

The code that I'm using to create the server on Node.JS is 我用来在Node.JS上创建服务器的代码是

var app      = new express();
var options = {
        key:    fs.readFileSync("/etc/letsencrypt/live/play.example.me/privkey.pem"),
        cert:   fs.readFileSync("/etc/letsencrypt/live/play.example.me/cert.pem"),
        ca:     fs.readFileSync("/etc/letsencrypt/live/play.example.me/chain.pem"),
        requestCert:        false,
        rejectUnauthorized: false
    };

var server      = require("https").createServer(options, app);
var io          = require("socket.io").listen(server, { serveClient: false });

I'm actually using ProxyPass to handle the subdomain "api." 我实际上是使用ProxyPass来处理子域“ api”。 that is passed to Node.JS / Express: 传递给Node.JS / Express:

<VirtualHost *:80>
    ServerName      api.example.me
    ServerAlias     example.me

    ProxyRequests off

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

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

This is working, but the webosocket not. 这是可行的,但webosocket无效。 Without Apache (running directly on Express) all works fine including the websocket. 没有Apache(直接在Express上运行),包括websocket在内的所有组件都可以正常工作。 But, I need to run Apache too because the website is written in PHP. 但是,我也需要运行Apache,因为该网站是用PHP编写的。

Am I doing something wrong? 难道我做错了什么?

You can use Apache Module mod_proxy_wstunnel to use ProxyPass with WebSocket traffic, for example: 您可以使用Apache Module mod_proxy_wstunnel将ProxyPass与WebSocket通信一起使用,例如:

ProxyPass "/ws2/"  "ws://echo.websocket.org/"
ProxyPass "/wss2/" "wss://echo.websocket.org/"

That said, there is no real need to proxy websocket traffic through Apache. 也就是说,并没有真正需要通过Apache代理Websocket通信的需求。 It uses a different Port to HTTP(S) traffic, so there is no real need to. 它使用不同的端口来传输HTTP(S)流量,因此没有必要。 You should just be able to use a library like socket.io and away you go. 您应该只能够使用socket.io之类的库就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM