我一直在尝试用我正在做的一个node.js项目来设置HTTPS。我基本上遵循了这个例子的node.js文档:

// curl -k https://localhost:8000/
var https = require('https');
var fs = require('fs');

var options = {
  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};

https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}).listen(8000);

现在,当我做的时候

curl -k https://localhost:8000/

我得到

hello world

像预期的那样。但如果我这样做了

curl -k http://localhost:8000/

我得到

curl: (52) Empty reply from server

回想起来,这似乎是显而易见的,它将以这种方式工作,但与此同时,最终访问我的项目的人不会输入https://yadayada,我希望所有的流量从他们访问网站的那一刻起就使用https。

我怎么能得到节点(和Express,因为这是我正在使用的框架)把所有传入的流量交给https,不管它是否被指定?我还没有找到任何解决这个问题的文档。或者只是假设在生产环境中,节点在它前面有一些东西(例如nginx)来处理这种重定向?

这是我第一次尝试web开发,所以如果这是显而易见的,请原谅我的无知。


当前回答

var express = require('express');
var app = express();

app.get('*',function (req, res) {
    res.redirect('https://<domain>' + req.url);
});

app.listen(80);

这就是我们所使用的,而且效果非常好!

其他回答

这里的大多数答案都建议使用req.headers.host头文件。

Host报头是HTTP 1.1所要求的,但它实际上是可选的,因为报头可能不是由HTTP客户端实际发送的,node/express将接受这个请求。

你可能会问:哪个HTTP客户端(例如:浏览器)可以发送一个缺少报头的请求?HTTP协议非常简单。您可以用几行代码编写一个HTTP请求,以不发送主机标头,如果每次接收到格式错误的请求时都抛出一个异常,根据处理此类异常的方式,这可能会导致服务器宕机。

所以总是验证所有输入。这不是妄想症,我在我的服务中收到过缺少主机头的请求。

另外,永远不要把url当作字符串。使用node url模块修改字符串的特定部分。将url视为字符串可以通过许多许多方式加以利用。不要这样做。

从0.4.12开始,我们还没有使用Node的HTTP/HTTPS服务器在同一个端口上监听HTTP和HTTPS的真正干净的方法。

有些人通过让Node的HTTPS服务器(这也适用于Express.js)侦听443(或其他端口),并让一个小型http服务器绑定到80并将用户重定向到安全端口来解决这个问题。

如果你必须能够在一个端口上处理这两个协议,那么你需要把nginx, lighttpd, apache,或其他一些web服务器放在那个端口上,并充当Node的反向代理。

如果你的节点应用程序安装在IIS上,你可以在web.config中这样做

<configuration>
    <system.webServer>

        <!-- indicates that the hello.js file is a node.js application 
    to be handled by the iisnode module -->

        <handlers>
            <add name="iisnode" path="src/index.js" verb="*" modules="iisnode" />
        </handlers>

        <!-- use URL rewriting to redirect the entire branch of the URL namespace
    to hello.js node.js application; for example, the following URLs will 
    all be handled by hello.js:
    
        http://localhost/node/express/myapp/foo
        http://localhost/node/express/myapp/bar
        -->
        <rewrite>
            <rules>
                <rule name="HTTPS force" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="^OFF$" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
                </rule>
                <rule name="sendToNode">
                    <match url="/*" />
                    <action type="Rewrite" url="src/index.js" />
                </rule>
            </rules>
        </rewrite>

        <security>
            <requestFiltering>
                <hiddenSegments>
                    <add segment="node_modules" />
                </hiddenSegments>
            </requestFiltering>
        </security>

    </system.webServer>
</configuration>

我找到了req。协议工作时,我使用express(没有测试,但我怀疑它工作)。使用当前节点0.10.22和express 3.4.3

app.use(function(req,res,next) {
  if (!/https/.test(req.protocol)){
     res.redirect("https://" + req.headers.host + req.url);
  } else {
     return next();
  } 
});

杰克的答案更新了代码。在https服务器旁边运行这个程序。

// set up plain http server
var express = require('express');
var app = express();
var http = require('http');

var server = http.createServer(app);

// set up a route to redirect http to https
app.get('*', function(req, res) {
  res.redirect('https://' + req.headers.host + req.url);
})

// have it listen on 80
server.listen(80);