【问题标题】:NodeJs Express: Listeining from HTTPS in Amazon EC2 UbuntuNodeJs Express:在 Amazon EC2 Ubuntu 中从 HTTPS 监听
【发布时间】:2017-10-21 13:15:42
【问题描述】:

我们将 Apache Server 和 NodeJs (Express) 组合用于网站。 没有 SSL,Apache 过去监听 80 端口,Nodejs 监听 8080 端口。

现在,我正在网站上安装 SSL。 Apache 设置为监听端口 443。我试图让 Nodejs 监听端口 8443、3333 等 - 将 AWS 安全组设置为端口的“自定义 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 服务器的地方,没有显示任何错误。

感谢任何见解。

在 Arif 指出这可以通过 Apache 设置解决后,我在

处添加了 Virtualhost 设置

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

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        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 webmaster@localhost
        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>

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

【问题讨论】:

  • 您必须使用https 来发出https://www.example.com:8443 的请求。如果您正在请求https://www.example.com:8443,则没有 apache 的角色。
  • 你可以向 EC2 中的 nodejs 服务器 (curl -Ik https://127.0.0.1:8443) 发出请求吗?你之前有没有在iptables里加过规则?
  • @ArifKhan 对不起,我的错。它是 https://...
  • @ronald8192 我得到以下信息:HTTP/1.1 404 Not Found X-Powered-By: Express Content-Security-Policy: default-src 'self' X-Content-Type-Options: nosniff Content -类型:文本/html; charset=utf-8 内容长度:140 日期:星期六,2017 年 10 月 21 日 23:13:59 GMT 连接:保持活动............ ..不,我从来没有这样做过。
  • @Sam11 如果你没有被找到,这意味着你没有实现/ GET 路线

标签: node.js express amazon-ec2 https


【解决方案1】:

首先你需要通过以下命令启用 ssl 和反向代理 apache 模块

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

然后在下面更新 apache ssl config(/etc/apache2/sites-available/default-ssl.conf) 文件

<IfModule mod_ssl.c>
    <VirtualHost _default_:443>
        ServerAdmin admin@example.com
        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

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

【讨论】:

  • 感谢您的回答,阿里夫。我试过这个,但它似乎并没有解决问题。我正在编辑我的问题以添加虚拟主机设置。
【解决方案2】:

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

另外,我们需要添加

({secure: true})

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

【讨论】:

    猜你喜欢
    • 2016-05-17
    • 2018-12-03
    • 2011-06-03
    • 1970-01-01
    • 2014-08-04
    • 2014-04-03
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    相关资源
    最近更新 更多