简体   繁体   English

通过SSL的Apache和NodeJS

[英]Apache and NodeJS over SSL

I need to switch from HTTP to HTTPS to be able to use the getUsermedia for webrtc implementation. 我需要从HTTP切换到HTTPS,才能使用getUsermedia进行webrtc实现。 I've installed certificates on my Apache version 2.2.22 and got it working. 我已经在我的Apache 2.2.22版本上安装了证书,并使其正常运行。

In the website I'm hosting NodeJS is used. 在我托管的网站中,使用了NodeJS。 Perhaps not the ideal setup but I've encountered problems when socket.io on the client wants to communicate with the NodeJS server. 也许不是理想的设置,但是当客户端上的socket.io要与NodeJS服务器通信时遇到了问题。

In the console this message appears: 在控制台中,此消息出现:

GET https://mydomain.nl:1900/socket.io/?EIO=3&transport=polling&t=1449219985177-166 net::ERR_CONNECTION_CLOSED 

It's obvious to me that the NodeJS server app can't handle requests over HTTPS because it's an HTTP server. 对我来说很明显,NodeJS服务器应用程序无法通过HTTPS处理请求,因为它是HTTP服务器。

I want to alter that server to be able to serve HTTPS requests using the SSL already in place on the apache webserver. 我想更改该服务器,使其能够使用apache网络服务器上已经存在的SSL来满足HTTPS请求。

Is this possible at all? 这有可能吗?

I recommend to have a look apache's mod_proxy . 我建议看看apache的mod_proxy This would allow you to run the external requests over the apache SSL pipeline. 这将允许您通过apache SSL管道运行外部请求。 The node.js can run only on the localhost interface without ssl support because it's proxy'd by apache. node.js只能在没有ssl支持的localhost接口上运行,因为它是由apache代理的。 I you configure a vhost it could look like this: 我配置了一个虚拟主机,它看起来可能像这样:

<VirtualHost *:443>
  ServerName www.yourserver.com
  DocumentRoot /var/www/vhosts/www.yourserver.com/public_html

  CustomLog <LOG-PATH> combined
  ErrorLog <ERROR-LOG-PATH>

  # Example SSL configuration
  SSLEngine on
  SSLProtocol all -SSLv2
  SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
  SSLCertificateFile "<CERT-PATH>/server.crt"
  SSLCertificateKeyFile "<CERT-PATH>/server.key"

  ProxyPass /<PATH>/ http://localhost:1900/
  ProxyPassReverse /<PATH>/ http://myserver:1900/
</VirtualHost>

Your reguest would then look like this: 您的要求将如下所示:

GET https://mydomain.nl/<PATH>/?.... 

Where <PATH> has the same value as in the vhost config's <ProxyPass> directive. 其中<PATH>与vhost配置的<ProxyPass>指令中的值相同。

Solved it by creating a seperate Vhost as proxy for the Node js. 通过创建单独的Vhost作为Node js的代理来解决它。 Named it socket.mydomain.nl and everything works well 命名为socket.mydomain.nl,一切正常

proxy nodejs 代理节点

var proxy = require('http-proxy').createProxyServer();
var fs = require('fs');

express = require('express.io');
app = express();


var SSloptions = {
    key:    fs.readFileSync('/var/www/node/certificados/mig.xxx.key'),
    cert:   fs.readFileSync('/var/www/node/certificados/xxx.crt'),    
    ca: [
        fs.readFileSync('/var/www/node/certificados/gd_bundle-xx.crt')
    ],
    rejectUnauthorized: false,
    requestCert: true,
    agent: false,
    strictSSL: false
};


app.https(SSloptions).io();


app.all('*', function(req, res){
    proxy.web(req, res, {

        target: 'http://localhost:4443',
    });    
});



app.listen(14443);

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

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