gpt4 book ai didi

ajax - Javascript 从 https 服务器获取请求到本地主机 :port with self signed SSL

转载 作者:太空宇宙 更新时间:2023-11-03 14:05:28 26 4
gpt4 key购买 nike

我在我的 Debian 服务器上配置并运行了两台服务器。一台主服务器和一台Elasticsearch (搜索引擎)服务器。

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

一切似乎都已正确配置,因为当我从服务器终端向 https://localhost:9999 执行 curl 时,我可以从服务器获得成功的响应。使用 -k 选项绕过自签名证书的验证,没有它,它不起作用。

我无法从我的 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 请求看起来像 curl -k https://localhost:9999/--user username:password

So, the actual question:

我希望能够对此服务器执行一个简单的 jQuery AJAX 请求。我正在尝试以下请求 $.get('https://username:password@localhost:9999/') 但我收到了 ERR_CONNECTION_REFUSED

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

有什么简单的方法可以用请求 header 或类似的东西解决这个问题吗?或者我需要购买 CA 证书才能使用 AJAX 吗?

最佳答案

你是对的,问题是自签名证书。如果你尝试相同的请求,但作为 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

如果您想不使用 ES 进行简单的 ajax 调用,您唯一可以做的就是提示用户访问该页面并在请求被拒绝时自行接受证书。

另见:https://stackoverflow.com/a/4566055/5758328

关于ajax - Javascript 从 https 服务器获取请求到本地主机 :port with self signed SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42621586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com