在Node中使用http-proxy时,UNABLE_TO_VERIFY_LEAF_SIGNATURE [英] UNABLE_TO_VERIFY_LEAF_SIGNATURE while using http-proxy in Node

查看:260
本文介绍了在Node中使用http-proxy时,UNABLE_TO_VERIFY_LEAF_SIGNATURE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部

我正在尝试创建一个简单的代理,该代理将所有请求详细信息转发到另一台服务器.为此,我使用了"http-proxy" npm.我正在尝试从本地服务器转到云服务器.首先,当我设置http-proxy时,我看到一个错误无法验证第一个证书".经过一些在线研究,我发现这可能与我拥有自签名证书的事实有关.由于它是自签名的,因此不在证书存储区中,因此无法进行验证.但是,因为在开发过程中不需要此功能,所以我添加了安全:虚假"以忽略证书验证.我知道从生产上讲这是不安全的,但是我现在只是想解决这个问题.此更新实际上解决了此错误.

I am trying to create a simple proxy which forwards all requests verbatum to another server. To do this I'm using the "http-proxy" npm. I am trying to go from local to a cloud server. At first when I setup the http-proxy I saw an error "unable to verify the first certificate". After some research online I found it's probably related to the fact that I have a self-signed certificate. Because it's self-signed it's not in the certificate store and so can't be validated. But, beacause I don't need this during development, I added "secure: false" to ignore certificate verification. I know that's unsafe from production, but I'm just trying to get around this for now. This update actually got around this error.

现在,我收到另一个错误"UNABLE_TO_VERIFY_LEAF_SIGNATURE".

Now, I am getting another error "UNABLE_TO_VERIFY_LEAF_SIGNATURE".

任何人都可以帮助我弄清楚如何消除此错误吗?我尝试添加以下内容: process.env ['NODE_TLS_REJECT_UNAUTHORIZED'] ='0'

Can any one help me figure out how to get rid of this error? I've tried adding this: process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'

但是那仍然显示错误.我看到了由http-proxy发出的事件中的错误(有关此事件,请参见下面的代码).如果我不使用'proxyRes',则可以在proxyRes->连接-> authorizationError-> UNABLE_TO_VERIFY_LEAF_SIGNATURE

But that still shows the error. I see the error in the event emitted by the http-proxy (See code below for this event). If I drill down itno the 'proxyRes' I can see this error in the proxyRes -> connection -> authorizationError -> UNABLE_TO_VERIFY_LEAF_SIGNATURE

这是我的下面的代码:

'use strict'
require('dotenv').config({silent: true})
var util = require('util');
const loggerFactory = require('./utils/logger')
const express = require('express')
const defaultRouter = require('./routes/default')
var logger = loggerFactory.consoleLogger
const proxy = require('http-proxy');

module.exports = (config) => {
const app = express()
// app.use(loggerFactory.requestLogger())
app.set('json spaces', 2)
app.set('port', config.express.port)

app.use('', defaultRouter)

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'
var apiProxy = proxy.createProxyServer({});
var proxyUrl = process.env.HOMEINSPECTIONSERVER_URL;

app.use((req,res,next) => {
apiProxy.web(req, res,
    {
      target: proxyUrl,
      secure: false,
    }
);

apiProxy.on('error', function(e) {
  logger.error("Error during proxy call!")
  logger.error("This is the error : " + e)
  next('route')
});

apiProxy.on('proxyReq', function(proxyReq, req, res, options) {
  logger.info("---REQUEST---")
  console.log("---REQUEST---")
  // logger.info(util.inspect(proxyReq))
   proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
});

apiProxy.on('proxyRes', function (proxyRes, req, res) {
  // logger.info("---RESPONSE---")
  // logger.info(util.inspect(proxyRes))
  // logger.info("---RESPONSEEND---")
  logger.info('RAW Response from the target', 
JSON.stringify(proxyRes.headers, true, 2));
});

apiProxy.on('open', function (proxySocket) {
  proxySocket.on('data', hybiParseAndLogMessage);
});

apiProxy.on('close', function (res, socket, head) {
  console.log('Client disconnected');
});

apiProxy.on('start', function (req, res, target) {
  // console.log('Started Request!');
});
})

app.use((req, res) => {
// logger.info('starting request...')
res.json(res.locals.standardResponse)
})
app.use((err, req, res, next) => {
var statusCode = 500

if (res.locals.standardResponse) {
  res.locals.standardResponse.error = err
  statusCode = err.statusCode || 600
  logger.error(err)
  res.status(statusCode).json(res.locals.standardResponse)
}

if (err.error !== undefined && err.error.httpStatus !== undefined) {
  statusCode = err.error.httpStatus
} else {
  statusCode = err.statusCode || 600
}

logger.error(err)
res.status(statusCode).json(res.body)
})

return app

}

推荐答案

对于上面也有此问题的任何人.我通过使用称为express-http-proxy的npm软件包解决了该问题.您可以在这里获得它:

For any one also having this problem above. I solved it by using the npm package called express-http-proxy. You can get it here:

在此处输入链接描述

所以我的代码现在看起来像这样:

So my code now looks like this:

'use strict'
require('dotenv').config({silent: true})
const loggerFactory = require('./utils/logger')
const express = require('express')
const defaultRouter = require('./routes/default')
var logger = loggerFactory.consoleLogger

module.exports = (config) => {
  process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'

  const app = express()
  app.set('json spaces', 2)
  app.set('port', config.express.port)
  app.use('', defaultRouter)
  var proxy = require('express-http-proxy');
  app.use(proxy(process.env.HOMEINSPECTIONSERVER_URL))

  return app
}

在这里注意重要的代码段:

Note the important piece of code here:

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'

希望能帮助任何陷入困境的人!

Hope that helps anyone who is stuck!

这篇关于在Node中使用http-proxy时,UNABLE_TO_VERIFY_LEAF_SIGNATURE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆