如何配置expressjs来处理http和https?

发布于 2024-12-11 19:17:54 字数 1182 浏览 0 评论 0原文

我已经搜索过 stackoverflow 和express google group,但仍然不够。

根据我收集的信息,我可以执行以下两件事之一:

1)创建 http 服务器和 https 服务器的实例,并将两者设置为侦听两个不同的端口。在路由中,将 http 请求重定向到 https 端口。

//app
var app = express.createServer();
var app_secure = express.createServer({key: key, cert: cert});

app.listen(8080);
app_secure.listen(8443);

//routes
app.get("unsecure/path", function(req, res) {
  ...
}

app.get("secure/path", function(req, res) {
  res.redirect("https://domain" + req.path);
}

app_secure.get("secure/path", function(req, res) {
  res.send("secure page");
}

2)按照TJ Hollowaychuk所说的去做: https://gist.github.com/1051583

var http = require("http");
var https = require("https");
var app = express.createServer({key: key, cert: cert});

http.createServer(app.handle.bind(app)).listen(8080);
https.createServer(app.handle.bind(app)).listen(8443);

当我做1时,通常没有问题。然而,管理两台服务器感觉很笨重,我真的觉得应该有更好的方法。

当我执行 2 时,我得到这个:

(node SSL) error:1408A0C1:SSLroutines:SSL3_GET_CLIENT_HELLO:no share cipher

当然,我可以默认使用选项 1,但我真的非常想知道为什么我会得到这个当我执行选项 2 时,“没有共享密码错误”。选项 2 将是我的首选路线。

I've scoured stackoverflow and the express google group, but I'm still coming up short.

From what I gather, I can do one of two things:

1) create an instance of an http server and an https server and set the two to listen to two different ports. In the routes, redirect the http request to the https port.

//app
var app = express.createServer();
var app_secure = express.createServer({key: key, cert: cert});

app.listen(8080);
app_secure.listen(8443);

//routes
app.get("unsecure/path", function(req, res) {
  ...
}

app.get("secure/path", function(req, res) {
  res.redirect("https://domain" + req.path);
}

app_secure.get("secure/path", function(req, res) {
  res.send("secure page");
}

2) do what TJ Hollowaychuk says: https://gist.github.com/1051583

var http = require("http");
var https = require("https");
var app = express.createServer({key: key, cert: cert});

http.createServer(app.handle.bind(app)).listen(8080);
https.createServer(app.handle.bind(app)).listen(8443);

When I do 1, there are generally no problems. However, it feels clunky to manage two servers and I really feel like there should be a better way.

When I do 2, I get this:

(node SSL) error:1408A0C1:SSL routines:SSL3_GET_CLIENT_HELLO:no shared cipher

Of course, I can just default to option 1, but I really, really want to know why I'm getting that "no shared cipher error" when I do option 2. And option 2 would be my preferred route.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぺ禁宫浮华殁 2024-12-18 19:17:54

按照@ypocat的评论,您可以在express.js应用程序中启用https,如下所示。

 var http = require('http');
 var https = require('https');
 var express = require('express');
 var fs = require('fs');

 var app = express.createServer();

 // cutomize your app as ususal
 app.configure( function () { ... });
 app.configure('production', function () { ... });
 // ....

 // attach express handler function to TWO servers, one for http and one for https
 http.createServer(app.handle.bind(app)).listen(8080);
 https.createServer({
   ca: fs.readFileSync('./server.ca-bundle'),
   key: fs.readFileSync('./server.key'),
   cert: fs.readFileSync('./server.crt')
 }, app.handle.bind(app)).listen(8081);

请注意,您应该从证书颁发机构接收server.ca-bundle、server.key和server.crt。

另外,由于您可能会在没有 sudo 的情况下运行节点,因此您需要确保端口 80(http) 和 443(https) 已打开

# in Ubuntu
sudo ufw status
sudo ufw allow 80
sudo ufw allow 443

,并分别将 8080 上的请求转发到 80 以及从 8081 转发到 443

# in Ubuntu
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8081

希望这有帮助

Following @ypocat 's comment you can enable https in your express.js application like so

 var http = require('http');
 var https = require('https');
 var express = require('express');
 var fs = require('fs');

 var app = express.createServer();

 // cutomize your app as ususal
 app.configure( function () { ... });
 app.configure('production', function () { ... });
 // ....

 // attach express handler function to TWO servers, one for http and one for https
 http.createServer(app.handle.bind(app)).listen(8080);
 https.createServer({
   ca: fs.readFileSync('./server.ca-bundle'),
   key: fs.readFileSync('./server.key'),
   cert: fs.readFileSync('./server.crt')
 }, app.handle.bind(app)).listen(8081);

Note that you should receive server.ca-bundle, server.key and server.crt from a certificate authority.

Also as you will probably run node without sudo you need to make sure port 80(http) and 443(https) are open

# in Ubuntu
sudo ufw status
sudo ufw allow 80
sudo ufw allow 443

and to forward requests on 8080 to 80 and from 8081 to 443 respectively

# in Ubuntu
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8081

Hope this helps

我恋#小黄人 2024-12-18 19:17:54

您的证书是 RSA 证书而不是 DSA 证书吗?听起来您的 NodeJS 服务器不支持您的浏览器支持的密码 - 您是否需要更新 OpenSSL 并重新编译 NodeJS?

Is your certificate an RSA certificate rather than a DSA one? It sounds like the ciphers your browser supports are not supported by your nodejs server - you many need to update your OpenSSL and recompile NodeJS?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文