Ala*_*lan 359 https node.js express
我正在尝试让HTTPS在express.js上运行节点,我无法弄明白.
这是我的app.js代码.
var express = require('express');
var fs = require('fs');
var privateKey = fs.readFileSync('sslcert/server.key');
var certificate = fs.readFileSync('sslcert/server.crt');
var credentials = {key: privateKey, cert: certificate};
var app = express.createServer(credentials);
app.get('/', function(req,res) {
res.send('hello');
});
app.listen(8000);
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它似乎只响应HTTP请求.
我写了简单的node.js基于香草的HTTPS应用程序:
var fs = require("fs"),
http = require("https");
var privateKey = fs.readFileSync('sslcert/server.key').toString();
var certificate = fs.readFileSync('sslcert/server.crt').toString();
var credentials = {key: privateKey, cert: certificate};
var server = http.createServer(credentials,function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
});
server.listen(8000);
Run Code Online (Sandbox Code Playgroud)
当我运行这个应用程序时,它会响应HTTPS请求.请注意,我不认为fs上的toString()结果很重要,因为我已经使用了两者的组合而仍然没有es bueno.
编辑添加:
对于生产系统,您最好使用Nginx或HAProxy来代理对nodejs应用程序的请求.您可以设置nginx来处理ssl请求,只需向您的节点app.js说http.
编辑添加(4/6/2015)
对于使用AWS的系统,最好使用EC2 Elastic Load Balancers来处理SSL终止,并允许常规HTTP流量到您的EC2 Web服务器.为了提高安全性,设置您的安全组,使得只有ELB允许HTTP流量发送到EC2实例,这将阻止打你的机器外部的未加密的HTTP流量.
cod*_*me- 604
在express.js中(从版本3开始),您应该使用该语法:
var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey = fs.readFileSync('sslcert/server.key', 'utf8');
var certificate = fs.readFileSync('sslcert/server.crt', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();
// your express configuration here
var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
httpServer.listen(8080);
httpsServer.listen(8443);
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您可以向本机http/https服务器提供快速中间件
如果您希望应用程序在1024以下的端口上运行,则需要使用sudo命令(不推荐)或使用反向代理(例如nginx,haproxy).
Der*_*gar 30
首先,您需要创建selfsigned.key和selfsigned.crt文件.转到创建自签名SSL证书
转到终端并运行以下命令.
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ./selfsigned.key -out selfsigned.crt
创建后,在代码中添加key&cert文件,并将选项传递给服务器.
const express = require('express');
const https = require('https');
const fs = require('fs');
const port = 3000;
var key = fs.readFileSync(__dirname + '/../certs/selfsigned.key');
var cert = fs.readFileSync(__dirname + '/../certs/selfsigned.crt');
var options = {
key: key,
cert: cert
};
app = express()
app.get('/', (req, res) => {
res.send('Now using https..');
});
var server = https.createServer(options, app);
server.listen(port, () => {
console.log("server starting on port : " + port)
});
Run Code Online (Sandbox Code Playgroud)
小智 25
我遇到了类似的问题,让SSL在端口443以外的端口上工作.在我的情况下,我有一个捆绑证书以及证书和密钥.捆绑证书是一个包含多个证书的文件,节点要求您将这些证书分解为数组的单独元素.
var express = require('express');
var https = require('https');
var fs = require('fs');
var options = {
ca: [fs.readFileSync(PATH_TO_BUNDLE_CERT_1), fs.readFileSync(PATH_TO_BUNDLE_CERT_2)],
cert: fs.readFileSync(PATH_TO_CERT),
key: fs.readFileSync(PATH_TO_KEY)
};
app = express()
app.get('/', function(req,res) {
res.send('hello');
});
var server = https.createServer(options, app);
server.listen(8001, function(){
console.log("server running at https://IP_ADDRESS:8001/")
});
Run Code Online (Sandbox Code Playgroud)
在app.js中,您需要指定https并相应地创建服务器.此外,请确保您尝试使用的端口实际上允许入站流量.
Dra*_*ire 13
这个答案与 Setthase 非常相似,但它适用于LetsEncrypt (Ubuntu)
// Dependencies
const fs = require('fs');
const http = require('http');
const https = require('https');
const express = require('express');
const app = express();
// Certificate
const privateKey = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/privkey.pem', 'utf8');
const certificate = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/cert.pem', 'utf8');
const ca = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/chain.pem', 'utf8');
const credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
app.use((req, res) => {
res.send('Hello there !');
});
// Starting both http & https servers
const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);
httpServer.listen(80, () => {
console.log('HTTP Server running on port 80');
});
httpsServer.listen(443, () => {
console.log('HTTPS Server running on port 443');
});
Run Code Online (Sandbox Code Playgroud)
您可能会遇到: EACCES:权限被拒绝,打开 '/etc/letsencrypt/live/yourdeomain.com/privkey.pem'
答案就在这里:让我们加密 SSL 无法启动“错误:EACCES:权限被拒绝,打开‘/etc/letsencrypt/live/domain.net/privkey.pem’”
对我有用的是 ubuntu ssh 终端中的 Get userwhoami
// Create group with root and nodeuser as members
$ sudo addgroup nodecert
$ sudo adduser ubuntu nodecert
$ sudo adduser root nodecert
// Make the relevant letsencrypt folders owned by said group.
$ sudo chgrp -R nodecert /etc/letsencrypt/live
$ sudo chgrp -R nodecert /etc/letsencrypt/archive
// Allow group to open relevant folders
$ sudo chmod -R 750 /etc/letsencrypt/live
$ sudo chmod -R 750 /etc/letsencrypt/archive
sudo reboot
Run Code Online (Sandbox Code Playgroud)
Nis*_*ani 10
在Sailsjs中,有两种方法可以配置所有的东西,首先是在config文件夹中配置,每个都有自己独立的文件(比如关于设置的数据库连接位于connections.js中).第二个是在环境基础文件结构上配置,每个环境文件都存在于config/env文件夹中,每个文件包含特定环境的设置.
Sails首先查看config/env文件夹,然后期待config/*.js
现在让我们设置ssl config/local.js.
var local = {
port: process.env.PORT || 1337,
environment: process.env.NODE_ENV || 'development'
};
if (process.env.NODE_ENV == 'production') {
local.ssl = {
secureProtocol: 'SSLv23_method',
secureOptions: require('constants').SSL_OP_NO_SSLv3,
ca: require('fs').readFileSync(__dirname + '/path/to/ca.crt','ascii'),
key: require('fs').readFileSync(__dirname + '/path/to/jsbot.key','ascii'),
cert: require('fs').readFileSync(__dirname + '/path/to/jsbot.crt','ascii')
};
local.port = 443; // This port should be different than your default port
}
module.exports = local;
Run Code Online (Sandbox Code Playgroud)
另外,您也可以在config/env/production.js中添加它.(此片段还显示了如何处理多个CARoot certi)
或者在production.js中
module.exports = {
port: 443,
ssl: {
secureProtocol: 'SSLv23_method',
secureOptions: require('constants').SSL_OP_NO_SSLv3,
ca: [
require('fs').readFileSync(__dirname + '/path/to/AddTrustExternalCARoot.crt', 'ascii'),
require('fs').readFileSync(__dirname + '/path/to/COMODORSAAddTrustCA.crt', 'ascii'),
require('fs').readFileSync(__dirname + '/path/to/COMODORSADomainValidationSecureServerCA.crt', 'ascii')
],
key: require('fs').readFileSync(__dirname + '/path/to/jsbot.key', 'ascii'),
cert: require('fs').readFileSync(__dirname + '/path/to/jsbot.crt', 'ascii')
}
};
Run Code Online (Sandbox Code Playgroud)
这里是Web Socket和wss代表Secure Web Socket,因为我们现在设置ssl http和ws两个请求变得安全并分别转换为https和wss.
我们的应用程序有很多来源会收到任何博客文章,社交媒体发布的请求,但我们的服务器只在https上运行所以当任何来自http的请求它在客户端浏览器中给出"此站点无法访问"错误.我们失去了网站流量.所以我们必须将http请求重定向到https,同样的规则允许websocket,否则socket将失败.
因此,我们需要在端口80(http)上运行相同的服务器,并将所有请求转移到端口443(https).在提升服务器之前,Sails首先编译config/bootstrap.js文件.在这里,我们可以在端口80上启动快速服务器.
在config/bootstrap.js中(创建http服务器并将所有请求重定向到https)
module.exports.bootstrap = function(cb) {
var express = require("express"),
app = express();
app.get('*', function(req, res) {
if (req.isSocket)
return res.redirect('wss://' + req.headers.host + req.url)
return res.redirect('https://' + req.headers.host + req.url)
}).listen(80);
cb();
};
Run Code Online (Sandbox Code Playgroud)
现在您可以访问http://www.yourdomain.com,它将重定向到https://www.yourdomain.com
小智 9
这就是它对我的工作方式。使用的重定向也将重定向所有正常的 http。
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const http = require('http');
const app = express();
var request = require('request');
//For https
const https = require('https');
var fs = require('fs');
var options = {
key: fs.readFileSync('certificates/private.key'),
cert: fs.readFileSync('certificates/certificate.crt'),
ca: fs.readFileSync('certificates/ca_bundle.crt')
};
// API file for interacting with MongoDB
const api = require('./server/routes/api');
// Parsers
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Angular DIST output folder
app.use(express.static(path.join(__dirname, 'dist')));
// API location
app.use('/api', api);
// Send all other requests to the Angular app
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
app.use(function(req,resp,next){
if (req.headers['x-forwarded-proto'] == 'http') {
return resp.redirect(301, 'https://' + req.headers.host + '/');
} else {
return next();
}
});
http.createServer(app).listen(80)
https.createServer(options, app).listen(443);
Run Code Online (Sandbox Code Playgroud)
Greenlock开箱即用地处理证书的发行和续签(通过Let's Encrypt)和http => https重定向。
express-app.js:
var express = require('express');
var app = express();
app.use('/', function (req, res) {
res.send({ msg: "Hello, Encrypted World!" })
});
// DO NOT DO app.listen()
// Instead export your app:
module.exports = app;
Run Code Online (Sandbox Code Playgroud)
server.js:
require('greenlock-express').create({
// Let's Encrypt v2 is ACME draft 11
version: 'draft-11'
, server: 'https://acme-v02.api.letsencrypt.org/directory'
// You MUST change these to valid email and domains
, email: 'john.doe@example.com'
, approveDomains: [ 'example.com', 'www.example.com' ]
, agreeTos: true
, configDir: "/path/to/project/acme/"
, app: require('./express-app.j')
, communityMember: true // Get notified of important updates
, telemetry: true // Contribute telemetry data to the project
}).listen(80, 443);
Run Code Online (Sandbox Code Playgroud)
观看QuickStart演示:https : //youtu.be/e8vaR4CEZ5s
只是提前回答这个问题,因为这是一个常见的后续问题:
您不能在本地主机上拥有SSL证书。但是,您可以使用Telebit之类的工具,使您可以将本地应用程序作为真实应用程序运行。
您还可以通过DNS-01挑战将私有域与Greenlock一起使用,这在README中已提及,并提供了支持它的各种插件。
阅读以上有关localhost的注释-您也不能通过Let's Encrypt使用非标准端口。
但是,您可以通过端口转发,sni-route将内部非标准端口公开为外部标准端口,或者使用Telebit之类的东西为您执行SNI路由和端口转发/中继。
您还可以使用DNS-01挑战,在这种情况下,您根本不需要公开端口,也可以通过这种方式保护私有网络上的域。
| 归档时间: |
|
| 查看次数: |
319174 次 |
| 最近记录: |