2017-03-06 15 views
0

我有两个配置的服务器和运行OM我的Debian服务器。一台主服务器和一台Elasticsearch(搜索引擎)服务器。的Javascript得到HTTPS服务器为localhost要求:自签署端口SSL

主服务器在具有NGINX代理和购买的SSL证书的https节点服务器上运行。 Elasticsearch服务器正在http服务器上运行。我添加了一个新的NGINX代理服务器https://localhost:9999重定向到http://localhost:9200使用自签名的SSL证书。 Elasticsearch服务器上还有一个使用用户名和密码的已配置身份验证。

一切似乎是正确配置,因为我可以从服务器获取成功响应时,我做的从服务器端卷曲朝https://localhost:9999-k选项绕过自签名证书的verication没有它,它不起作用。

我无法从我的https主服务器向我的http本地主机服务器执行跨域请求。因此我需要在我的本地主机服务器上配置https。


没有-k选项:

curl: (60) SSL certificate problem: self signed certificate 
More details here: http://curl.haxx.se/docs/sslcerts.html 

curl performs SSL certificate verification by default, using a "bundle" 
of Certificate Authority (CA) public keys (CA certs). If the default 
bundle file isn't adequate, you can specify an alternate file 
using the --cacert option. 
If this HTTPS server uses a certificate signed by a CA represented in 
the bundle, the certificate verification probably failed due to a 
problem with the certificate (it might be expired, or the name might 
not match the domain name in the URL). 
If you'd like to turn off curl's verification of the certificate, use 
the -k (or --insecure) option. 

随着-k选项:

{ 
    "name" : "server-name", 
    "cluster_name" : "name", 
    "cluster_uuid" : "uuid", 
    "version" : { 
    "number" : "x.x.x", 
    "build_hash" : "abc123", 
    "build_date" : "Timestamp", 
    "build_snapshot" : false, 
    "lucene_version" : "x.x.x" 
    }, 
    "tagline" : "You Know, for Search" 
} 

这是一个成功的Elasticsearch服务器响应。


所以完整的卷曲请求看起来像curl -k https://localhost:9999/ --user username:password

所以,实际的问题:

我希望能够做到对这个服务器的简单的jQuery AJAX请求。我正在尝试以下要求$.get('https://username:[email protected]:9999/')但我得到ERR_CONNECTION_REFUSED

我的猜测是,AJAX请求不会绕过自签名证书验证,因此它拒绝连接。

是否有请求头之类的东西来解决这个问题没有简单的方法?或者我是否需要购买CA证书才能使用AJAX工作?

回答

1

你是对的问题是自签名的证书。如果你尝试相同的请求,但作为http它将工作。

这里是一个解决办法,使ElasticSearch以https工作:

你需要实现自己的HTTP连接器:

var HttpConnector = require('elasticsearch/src/lib/connectors/http'); 
var inherits = require('util').inherits; 
var qs = require('querystring'); 
var fs = require('fs'); 

function CustomHttpConnector(host, config) { 
    HttpConnector.call(this, host, config); 
} 

inherits(CustomHttpConnector, HttpConnector); 

// This function is copied and modified from elasticsearch-js/src/lib/connectors/http.js 
CustomHttpConnector.prototype.makeReqParams = function (params) { 
    params = params || {}; 
    var host = this.host; 

    var reqParams = { 
    method: params.method || 'GET', 
    protocol: host.protocol + ':', 
    auth: host.auth, 
    hostname: host.host, 
    port: host.port, 
    path: (host.path || '') + (params.path || ''), 
    headers: host.getHeaders(params.headers), 
    agent: this.agent, 
    rejectUnauthorized: true, 
    ca: fs.readFileSync('publicCertificate.crt', 'utf8') 
    }; 

    if (!reqParams.path) { 
    reqParams.path = '/'; 
    } 

    var query = host.getQuery(params.query); 
    if (query) { 
    reqParams.path = reqParams.path + '?' + qs.stringify(query); 
    } 

    return reqParams; 
}; 

module.exports = CustomHttpConnector; 

然后注册它像这样:

var elasticsearch = require('elasticsearch'); 
var CustomHttpConnector = require('./customHttpConnector'); 

    var Elasticsearch = function() { 
     this.client = new elasticsearch.Client({ 
     host: { 
      host: 'my.server.com', 
      port: '443', 
      protocol: 'https', 
      auth: 'user:passwd' 
     }, 
     keepAlive: true, 
     apiVerison: "1.3", 
     connectionClass: CustomHttpConnector 
     }); 
    } 

https://gist.github.com/fractalf/d08de3b59c32197ccd65

如果你想简单的Ajax调用为u唱ES你可以做的唯一事情就是提示用户访问页面,并在请求被拒绝时自己接受证书。

另请参阅:https://stackoverflow.com/a/4566055/5758328

+0

非常感谢您的回复。我会试试这个并回复你。 –

+0

这是否意味着我必须向我的节点后端发出请求并向es发送请求并将响应发送回前端?或者我可以用纯粹的前端解决这个问题吗?我不是100%确定这个例子中发生了什么:)我可以用你的例子配置Elasticsearch并成功地ping通https服务器。 –

+0

本示例告诉ElasticSearch使用自定义连接类来执行它的ajax请求。查看我上面的编辑 – Mawcel

相关问题