Node.js反向代理后面的ArangoDB Web界面-无法连接

时间:2019-04-08 06:21:14

标签: node.js arangodb node-http-proxy

我有一个Node.js后端,用于身份验证和ArangoDB Web界面的反向代理。我一辈子都无法弄清楚为什么我无法使用外部URL登录Web界面。

我搜索过高和低(谷歌,堆栈溢出,arangodb git发出线程,arangodb应用程序代码等),但无法弄清楚。我不了解下面使用的node-http-proxy模块。如果有人在节点内部以其他方式完成此操作。

我已经看到了使用nginx等的示例,但是我实际上是试图将所有内容都保留在节点后端之下,以便能够将代理访问权限保留在我的站点身份验证之后,因此我不会将Web Interface暴露给随机访问。

我希望已清除此障碍的人能够提供帮助。

问题: _open / auth请求永远不会响应。我仍然可以从服务器访问http://localhost:8529并登录。

所需结果: 从http://example.com:8080/_db/_system/_admin/aardvark/index.html#login访问Web界面并成功登录。

[Chrome标头网络详细信息]:

Request URL: http://example.com:8080/_db/_system/_open/auth
Referrer Policy: no-referrer-when-downgrade

Request Headers:
Provisional headers are shown
Accept: application/json, text/javascript, */*; q=0.01
Authorization: bearer null
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Origin: http://example.com:8080
Referer: http://example.com:8080/_db/_system/_admin/aardvark/index.html
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36
X-Requested-With: XMLHttpRequest

Form Data:
{"username":"username","password":"password"}: 

有趣的是:http://127.0.0.1:8080/_db/_system/_admin/aardvark/foxxes/fishbowl总是出现401错误(即使在本地地址和端口的服务器上,也认为这是aardvark.js的错误)

请参阅以下配置文件。

[routes.js]:

// =====================================
// ArangoDB Web interface ============
// =====================================
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({followRedirects: true});

// set headers location overwrite per arangodb documentation
proxy.on('proxyReq', function(proxyReq, req, res, options) { 
    proxyReq.setHeader('X-Script-Name', 'http://example.com:8080');
});

proxy.on('error', function(e) {
    console.log(e);
});

app.all('*/_db/*', function(req, res) {
    proxy.web(req, res, {target: 'http://localhost:8529'});
});

[/ etc / arangodb3 / arangod.conf]:

[frontend]
proxy-request-check=false
version-check=false
[http]
trusted-origin=all
allow-method-override=true

注意:我也尝试过'trusted-origin = *'(不确定哪个是正确的)

1 个答案:

答案 0 :(得分:0)

已解决

希望这可以帮助其他人!

我完全错失了一个事实,那就是您必须手动将帖子数据从表单转发到代理(以为这样做会自动完成)。另外,当您获取帖子正文并在值的末尾附加一个:“”时,node / arango做了一些奇怪的事情。因此,您必须手动将其删除。

// =====================================
// ArangoDB query interface Route ========
// =====================================
// Add the following to the arangodb config file /etc/arangodb3/arangodb.conf
// [frontend]
// proxy-request-check = false
// [http]
// trusted-origin = *
// trusted-origin = all

// Create the proxy
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer();

// Catch requests, massage the login form json, and pass along to the proxied server
proxy.on('proxyReq', function(proxyReq, req, res, options) {
    var bodyData;
    var contentType = proxyReq.getHeader('Content-Type');

    if (!req.body || !Object.keys(req.body).length) {
        return;
    };
    if (contentType == 'application/json; charset=UTF-8' || contentType == 'application/json') {
        bodyData = JSON.stringify(req.body);
    };

    if (contentType == 'application/x-www-form-urlencoded; charset=UTF-8' || contentType == 'application/x-www-form-urlencoded') {
        bodyData = queryString.stringify(req.body);
    };

    // handle formatting issue with arangodb web form where it appends a :"" to the end of the credentials (i.e. {"username","root","password","password"}:"")
    if (bodyData) {
        if (req.url.substr(-23) == '/_db/_system/_open/auth') {
            proxyReq.write(String(Object.keys(req.body)[0]))
        } else {
            proxyReq.write(bodyData);
            proxyReq.end();
        };
    };
});

  proxy.on('error', function(e) {
      console.log(e);
  });

  // note I have two functions to ensure that a user is logged in and has a specific role before they can login to the database. Insert authentication functions / middleware before the function(req, res) below
  app.all('*/_db/*', function(req, res) { //, isLoggedIn, inRole('admin')
      proxy.web(req, res, {target: 'http://127.0.0.1:8529'});
  });