如何配置ng serve代理以支持Windows身份验证和HTTPS

5
我想使用ng serve和一个使用Windows身份验证(NTLM)保护的后端服务器。这与以下帖子中的情况非常相似(示例1示例2),只是服务器通过HTTPS访问。
当我尝试使用相同的解决方案(在HTTP下正常工作)时,我会收到一个404错误(但是我可以直接使用浏览器测试此URL访问服务器)。
const Agent = require("agentkeepalive");

module.exports = {
    '/api': {
        target: "https://my-server.example.com",
        secure: false,
        changeOrigin: true,
        agent: new Agent({
            maxSockets: 100,
            keepAlive: true,
            maxFreeSockets: 10,
            keepAliveMsecs: 100000,
            timeout: 6000000,
            keepAliveTimeout: 90000
        }),
        onProxyRes: proxyRes => {
            const key = "www-authenticate";
            proxyRes.headers[key] = proxyRes.headers[key] &&
                proxyRes.headers[key].split(",");
        }
    }
}

这里输入图片描述

任何帮助都将不胜感激,包括一些诊断问题的方法。

更新:使用logLevel = “debug”,我得到以下堆栈跟踪(由于Windows配置,相同的代理配置在HTTPS上运行良好,但我并不理解):

TypeError [ERR_INVALID_PROTOCOL]: Protocol "https:" not supported. Expected "http:"
    at new ClientRequest (_http_client.js:119:11)
    at Object.request (https.js:289:10)
    at Array.stream (C:\myproject\node_modules\http-proxy\lib\http-proxy\passes\web-incoming.js:126:74)      
    at ProxyServer.<anonymous> (C:\myproject\node_modules\http-proxy\lib\http-proxy\index.js:81:21)
    at middleware (C:\myproject\node_modules\http-proxy-middleware\lib\index.js:46:13)
    at handle (C:\myproject\node_modules\webpack-dev-server\lib\Server.js:322:18)
    at app.use (C:\myproject\node_modules\webpack-dev-server\lib\Server.js:330:47)
    at Layer.handle_error (C:\myproject\node_modules\express\lib\router\layer.js:71:5)
    at trim_prefix (C:\myproject\node_modules\express\lib\router\index.js:315:13)
    at C:\myproject\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\myproject\node_modules\express\lib\router\index.js:335:12)
    at next (C:\myproject\node_modules\express\lib\router\index.js:275:10)
    at Layer.handle [as handle_request] (C:\myproject\node_modules\express\lib\router\layer.js:97:5)
    at trim_prefix (C:\myproject\node_modules\express\lib\router\index.js:317:13)
    at C:\myproject\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\myproject\node_modules\express\lib\router\index.js:335:12)
1个回答

15

经常情况下,当我在 Stack Overflow 上发了一个问题,在经过数天的挣扎之后,在接下来的一小时内我就自己找到了解决方案...

问题是 Agent 只适用于 HTTP。对于 HTTPS,我们必须使用 HttpsAgent

const HttpsAgent = require('agentkeepalive').HttpsAgent;

module.exports = {
    '/api': {
        target: "https://my-server.example.com",
        secure: false,
        changeOrigin: true,
        agent: new HttpsAgent({
            maxSockets: 100,
            keepAlive: true,
            maxFreeSockets: 10,
            keepAliveMsecs: 100000,
            timeout: 6000000,
            keepAliveTimeout: 90000
        }),
        onProxyRes: proxyRes => {
            const key = "www-authenticate";
            proxyRes.headers[key] = proxyRes.headers[key] &&
                proxyRes.headers[key].split(",");
        }
    }
}

3
谢谢,我希望早点找到这个。 - Andrew Lichtenberg
哇,谢谢!我已经盯着屏幕读了很久并尝试了很多次... - Imposter
1
如果有人没有注意到的话:这是proxy.conf.js文件。 - g.pickardou
在我按照上述配置代理之后:.ng serve 写入:TypeError [ERR_INVALID_PROTOCOL]: 协议“https:”不受支持。预期为“http:” 在新的 NodeError (node:internal/errors:387:5) - g.pickardou

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接