node.js - node.js Express 服务器中的 SSL 正在运行但无法正常工作

标签 node.js apache ssl

我拥有的是用于 webadd.in 域的 Commodo Positive SSL SHA-2 证书

http://namecheap.com 提供给我的三个文件

webadd_in.crt
webadd_in.p7b
webadd_in.ca-bundle

我的 main.js 文件

var express = require('express');
var fs = require('fs');
var app = express();
var bodyParser = require("body-parser");
var https          =         require("https");
app.use(bodyParser.urlencoded({ extended: false }));

var config     = {
  key: fs.readFileSync('/etc/ssl/ssl.key/webadd_in.in.key'),
  cert: fs.readFileSync('/etc/ssl/ssl.crt/webadd_in.ca-bundle') //insert the the .ca-bundle file here
};
var port = 443;
app.set('port', port);

require('./routes')(app);
var server = https.createServer(config, app).listen(port, function(){
  console.log("Express server listening on port " + port);
});

我的代码运行但没有任何输出。没有错误没有警告什么都没有。 如果我删除 ssl 部分并更改端口,它就会工作

我在这台服务器上有 apache,但我有 a2dismod ssl 并且端口 443 确实是免费的

我还想知道是否可以在 443 以外的端口上运行此 Node 服务器,以便我可以像这样访问它 https://webadd.in:3030/

最佳答案

好的,如果您最终遇到这种情况,这里有可能的原因和解决方案。

注意:为了保持格式正确,我在最后放置了链接和命令。

  1. 如果您尝试使用给定的 ca-bundle 文件,请不要。创建证书链。向当局索取文件。
  2. 如果您 100% 确定 *.key 和 *.crt 是正确的文件,则它们的格式不正确。 [namecheap 为我提供了更正后的格式]。
  3. 要在不同端口上运行证书(证书可以是单个域),您需要使用 nginx 反向代理。然后您可以使用 node.js 和 php 以及任何其他东西。这也是一种非常好的方法,因为您可以两全其美。

由于链证书中的一些格式错误,我被卡住了,该错误已通过额外删除空格解决。

一些命令和资源

  1. 您可以在此处检查证书是否与 CSR 或私钥匹配 https://www.sslchecker.com/matcher
  2. 创建链证书的命令 cat domain.crt COMODO_DV_SHA-256_bundle.crt >> domain_cert_chain.crt
  3. nginx 配置文件应该是这样的。

notes: create a index.html file (can be blank) in node.js directory. configure apache to run on port 8080 on localhost only configure node to run on port 3030 on localhost only restart apache node and nginx nginx reverse proxy guide https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-reverse-proxy-for-apache

server {
  listen   80;
        listen 443;
        ssl off;
        server_name domain.in;
        ssl_certificate /etc/nginx/ssl/nginx_bundle.crt;
        ssl_certificate_key /etc/nginx/ssl/domain_in.key;
        root /var/www/html/;
        index index.php index.html index.htm;
        server_name domain.in;
        location / {
          try_files $uri $uri/ /index.php;
  }
  location ~ \.php$ {
          proxy_set_header X-Real-IP  $remote_addr;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header Host $host;
      proxy_pass http://127.0.0.1:8080;
  }
        location ~ /\.ht {
          deny all;
  }
}
server {
        listen 3035;
        listen 443;
        ssl on;
        server_name domain.in;
        ssl_certificate /etc/nginx/ssl/nginx_bundle.crt;
        ssl_certificate_key /etc/nginx/ssl/domain_in.key;
        root /var/www/domainnodeapi/;
        index index.html index.htm;
        access_log /var/log/nginx/nginx.vhost.access.log;
        error_log /var/log/nginx/nginx.vhost.error.lo;
        client_max_body_size 10G;
        location / {
            proxy_pass http://127.0.0.1:3030;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_buffering off;
       }
 }
  1. 命令重启nginx、apache

sudo nginx -s reload
sudo service nginx restart
sudo service apache restart

use pm2 for node

如果出现错误

nginx: [emerg] PEM_read_bio_X509_AUX("/etc/nginx/ssl/domain.crt") failed (SSL:)

是因为括号里的文件。检查格式错误。

如果出现 openssl 错误/版本未知/找不到文件:

sudo apt-get purge openssl libssl-dev
sudo apt-get install openssl libssl-dev

希望这对您有所帮助。

关于node.js - node.js Express 服务器中的 SSL 正在运行但无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33626544/

相关文章:

javascript - 将 json 从 Angular JS 传递到 Node JS

apache - 如何将 HTTP 永久重定向到 HTTPS SSL url?

php - 编码:一切都是 UTF-8,但 DB 输出显示错误。有任何想法吗?

javascript - 将数据保存在数据库问题中,在控制台上收到 NULL

javascript - 在nodejs中获取request.body值未定义?

c++ - 如何为我的 Apache 模块定义自定义配置指令?

python - 没有证书的 HTTPS 的 OpenSSL

ssl - 我应该什么时候初始化 https 连接?

java Web 服务客户端尝试通过 SSL 访问 Web 服务 - TrustManagerFactoryImpl 未初始化

javascript - 使用js过滤重复记录并添加附加条件插入数组