2017-03-06 39 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

相關問題