NodeJs Express:从Amazon EC2中的HTTPS进行监听Ubuntu

NodeJs Express:从Amazon EC2中的HTTPS进行监听Ubuntu

问题描述:

我们将Apache Server和NodeJs(Express)组合用于网站。 没有SSL,Apache用于侦听端口80和Nodejs到端口8080.NodeJs Express:从Amazon EC2中的HTTPS进行监听Ubuntu

现在,我正在网站上安装SSL。 Apache设置为侦听端口443.我尝试让Nodejs侦听端口8443,3333等。 - 将AWS Security Group设置为端口的“Custom TCP”。问题是我没有收到来自Nodejs服务器的任何响应。我的预感是问题出在EC2安全组设置上,但无法真正解决问题。

服务器用于创建的NodeJS如下:

var https = require('https'); 
var fs = require('fs'); 
... 
... 
var https_options = { 
     ca: fs.readFileSync ("/path/to/cabundle.ca-bundle", "utf8"), 
     key: fs.readFileSync("/path/to/sslkey.key", "utf8"), 
     cert: fs.readFileSync("/path/to/certificate.crt", "utf8") 
}; 


var app = express(); 
var server = https.createServer(https_options, app); 
.... 
.... 
server.listen(8443 or 3333); 

启动服务器不会造成任何错误。 浏览器,我从中调用Nodejs服务器,不显示任何错误。

任何洞察力是赞赏。

阿里夫指出,这可以通过Apache的设置来解决后,我在

/etc/apache2/sites-enabled/000-default.conf

<VirtualHost *:80> 
     ServerAdmin [email protected] 
     ServerName www.example.com 

     Redirect permanent/https://www.example.com 

     ErrorLog ${APACHE_LOG_DIR}/error.log 
     CustomLog ${APACHE_LOG_DIR}/access.log combined 

</VirtualHost> 

<VirtualHost *:443> 
     ServerAdmin [email protected] 
     DocumentRoot /var/www/html/mysite 
     Servername www.example.com 

     SSLEngine on 
     SSLCertificateKeyFile /path/to/sslkey.key 
     SSLCertificateFile /path/to/certificate.crt 
     SSLCertificateChainFile /path/to/cabundle.ca-bundle 


     <Directory /var/www/html/mysite> 
       AllowOverride All 
       Options FollowSymlinks 
     </Directory> 
</Virtualhost> 
添加虚拟主机seetings

客户端的请求被发送到https://www.example.com:8443

+0

您必须使用'https'来发出https://www.example.com:8443'的请求。如果你要求“https://www.example.com:8443”,那么就没有apache的作用。 –

+0

您可以向EC2中的nodejs服务器发出请求('curl -Ik https://127.0.0.1:8443')吗?你之前是否在'iptables'中添加了任何规则? – ronald8192

+0

@ArifKhan对不起,我的坏。它是https:// ... – Sam11

首先你需要启用SSL,并按照通讯反向代理的Apache模块和

sudo a2enmod ssl 
sudo a2enmod proxy 
sudo a2enmod proxy_http 
sudo a2enmod proxy_balancer 
sudo a2enmod lbmethod_byrequests 

,然后更新Apache的SSL配置(/etc/apache2/sites-available/default-ssl.conf)文件波纹管

<IfModule mod_ssl.c> 
    <VirtualHost _default_:443> 
     ServerAdmin [email protected] 
     ServerName your_domain.com 
     ServerAlias www.your_domain.com 
     ErrorLog ${APACHE_LOG_DIR}/error.log 
     CustomLog ${APACHE_LOG_DIR}/access.log combined 
     SSLEngine on 
     SSLCertificateFile /path/to/certificate.crt 
     SSLCertificateKeyFile /path/to/sslkey.key 
     ProxyRequests Off 
     ProxyPreserveHost On 
     ProxyVia Full 
     <Proxy *> 
      Require all granted 
     </Proxy> 
     <Location /> 
      ProxyPass http://localhost:8443 
      ProxyPassReverse http://localhost:8443 
     </Location> 
    </VirtualHost> 
</IfModule> 

现在添加SSL文件并重新启动Apache

sudo a2ensite default-ssl.conf 
sudo service apache2 restart 

现在应该正常工作https://your_domain.com

注意:您必须打开http和来自AWS控制台的安全组中的https端口

+0

谢谢你的答案阿里夫。我尝试过,但似乎没有解决问题。我正在编辑我的问题以添加虚拟主机设置。 – Sam11

对不起,延迟回复。问题在于不允许HTTPS和WSS流量的CSP设置。

此外,我们还需要添加

({secure: true}) 

同时使用socket.io创建客户端Socket。现在它工作正常。谢谢大家的意见。