开发者

node.js and nginx SSL handshake failure

I'm trying to create a basic SSL connection from a node.js app to a locally hosted nginx server; it involves sending the client's credentials as well. The handshake seems like it was successful as calling "verifyPeer" from within a "secure" event verifies that much. However, the server continues to respond with nothing but a 400 response.

If I make the same request with curl on the command line, I get back what I expect:

curl -v -E curl-test.crt --cacert ca.crt https://internal.url:7443/some.file

"curl-test.crt" was created by concatenating the client key and certificate together.

Here is the smallest bit of node.js code needed to get a failure:

global.util = require('util');

var fs = require('fs'),
    http = require('http'),
    crypto = require('crypto');

var clientCert = fs.readFileSync("tmp/cert.crt", 'ascii'),
    clientKey = fs.read开发者_如何学运维FileSync("tmp/key.key", 'ascii'),
    caCert = fs.readFileSync("tmp/ca.crt", 'ascii');
var credentials = crypto.createCredentials({"key": clientKey, "cert": clientCert, "ca": caCert});

var client = http.createClient(7443, "internal.url", true, credentials);

client.addListener("secure", function() {
  if (!client.verifyPeer()) {
    throw new Exception("Could not verify peer");
  }
});

var request = client.request('GET', '/some.file', {});

request.on('response', function(response) {
  response.on('data', function(body) {
    util.log("body: " + body);
  });
});

request.end();

And here is the response I get, no matter what "some.file" is changed to:

body: <html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/0.6.32</center>
</body>
</html>

Any help in debugging or solving this issue would be fantastic


Are you getting this message in your nginx error log?

2010/11/23 17:51:59 [info] 13221#0: *1 client sent HTTP/1.1 request without "Host" header while reading client request headers, client: 127.0.0.1, server: testme.local, request: "GET /some.file HTTP/1.1"

If so, you can fix it by simply adding the 'Host' header to your GET request like this:

var request = client.request('GET', '/some.file', {'Host':'internal.url'});

Looks like nginx wants the Host header and node doesn't send it by default. There's probably a way to configure nginx to default to the correct header value as well.

Hope that helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜