通过SSL / TLS连接访问Vagrant上的Node.js应用程序

所以我inheritance了一个Nodes.js应用程序,我在一个Vagrant框上运行。

我有应用程序绑定到“0.0.0.0”,它有自己的server.key和证书在securekey文件夹中。

var https = require('https'); var fs = require('fs'); var ssl_options = { key: fs.readFileSync('./securekey/server.key'), cert: fs.readFileSync('./securekey/server.crt'), ca: fs.readFileSync('./securekey/ca.crt') }; https.createServer(ssl_options, app).listen(3001, '0.0.0.0'); 

当我运行该应用程序时,我希望能够通过URL https:// localhost:3001 (在我的Windows PC上运行Vagrant)浏览器访问它

但是我在Mozilla上得到了“安全连接失败”。

我曾经使用Cygwin在Windows PC上试过这个:

 $ openssl s_client -host 127.0.0.1 -port 3001 CONNECTED(00000003) write:errno=104 --- no peer certificate available --- No client certificate CA names sent --- SSL handshake has read 0 bytes and written 316 bytes --- New, (NONE), Cipher is (NONE) Secure Renegotiation IS NOT supported Compression: NONE Expansion: NONE No ALPN negotiated SSL-Session: Protocol : TLSv1.2 Cipher : 0000 Session-ID: Session-ID-ctx: Master-Key: Key-Arg : None PSK identity: None PSK identity hint: None SRP username: None Start Time: 1461923745 Timeout : 300 (sec) Verify return code: 0 (ok) --- 

 $ curl -v -k 'https://localhost:3001' * STATE: INIT => CONNECT handle 0x6000574a0; line 1103 (connection #-5000) * Rebuilt URL to: https://localhost:3001/ * Added connection 0. The cache now contains 1 members * Trying 127.0.0.1... * STATE: CONNECT => WAITCONNECT handle 0x6000574a0; line 1156 (connection #0) * Connected to localhost (127.0.0.1) port 3001 (#0) * STATE: WAITCONNECT => SENDPROTOCONNECT handle 0x6000574a0; line 1253 (connection #0) * ALPN, offering h2 * ALPN, offering http/1.1 * Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH * successfully set certificate verify locations: * CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none * TLSv1.2 (OUT), TLS header, Certificate Status (22): * TLSv1.2 (OUT), TLS handshake, Client hello (1): * STATE: SENDPROTOCONNECT => PROTOCONNECT handle 0x6000574a0; line 1267 (connection #0) * Unknown SSL protocol error in connection to localhost:3001 * Curl_done * Closing connection 0 * The cache now contains 0 members curl: (35) Unknown SSL protocol error in connection to localhost:3001 

但是这些命令在Vagrant虚拟机terminal上运行时会返回成功的连接!

为了让我的Windows PC /浏览器接受应用程序的证书,我需要做些什么才能从Mozilla Firefox访问应用程序? 既然它已经有了server.key和certs,当然我不需要为应用程序再次生成我自己的密钥?

编辑:这是我的stream浪文件:

 Vagrant.configure(2) do |config| config.vm.box = "centos7" config.vm.network "forwarded_port", guest: 3000, host: 3000, auto_correct: true config.vm.network "forwarded_port", guest: 3001, host: 3001, auto_correct: true end 

我只有端口转发configuration..其余的是默认的。

当应用程序在Vagrant上运行时,netstat显示端口正在监听连接

 $ netstat -an | grep 3001 TCP 0.0.0.0:3001 0.0.0.0:0 LISTENING 

当我在浏览器上访问https:// localhost:3001时,我看到:

  netstat -an | grep 3001 TCP 0.0.0.0:3001 0.0.0.0:0 LISTENING TCP 127.0.0.1:3001 127.0.0.1:49651 ESTABLISHED TCP 127.0.0.1:49651 127.0.0.1:3001 ESTABLISHED 

好像端口连接好,但是vm不能返回数据。

经过大量的挖掘,我偶然发现了这个评论: https : //unix.stackexchange.com/a/255404

因为我在CentOS 7,禁用firewalld为我做了诡计。 我没有意识到这个变化。 从某种意义上说,joelnb在回答评论中检查iptables的说明是正确的方向(谢谢!)。 请检查您的操作系统的防火墙,并尝试禁用它,看看它是否有助于解决这个问题。 如果是的话,那么你可以继续为你的端口configuration一个规则。

对于CentOS 7, 打开firewalld上的端口 : centos 7 – 打开防火墙端口

我希望这有助于某人。

我怀疑你没有在你的Vagrantfile的端口转发设置,因为如果我没有/如果没有监听的端口,我得到了确切的错误。 你的stream浪文件看起来像下面这样吗? forwarded_port部分是重要的一点。

 Vagrant.configure(2) do |config| config.vm.box = "ubuntu/trusty64" config.vm.network "forwarded_port", guest: 3001, host: 3001 end 

否则,你可以请张贴您的Vagrantfile,我会修改我的答案。