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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Node.js反向代理背后的ArangoDB Web界面 - 无法连接相关的知识,希望对你有一定的参考价值。

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

我搜索了高低(谷歌,堆栈溢出,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/A让OD B3/A让OD.conf]:

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

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

答案

Solved

希望这有助于其他人!

我完全错过了你必须手动将表单数据从表单传递给代理的事实(认为它会自动执行此操作)。加上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'});
  });

以上是关于Node.js反向代理背后的ArangoDB Web界面 - 无法连接的主要内容,如果未能解决你的问题,请参考以下文章

安装Nginx并为node.js设置反向代理

Node.JS前面反向代理的优点

将 nginx 配置为 node.js 的反向代理有啥问题?

如果Node.js已具备反向代理的功能,我为什么要使用反向代理?

Node.js API 网关和 nginx 作为反向代理

Nginx 反向代理 Serving Node.js app 静态文件