简体   繁体   中英

Using Angular CLI to reverse proxy to a service in API Gateway/lambda

I am trying to reverse proxy Angular using the proxy.conf.json to a lambda behind API gateway.

{
    "/api/profile/*": {
        "target": "http://asdasdfsdf.execute-api.ap-southeast-2.amazonaws.com",
        "secure": false,
        "logLevel": "debug",
        "changeOrigin": false,
        "headers": {
            "host":"asdasdfsdf.execute-api.ap-southeast-2.amazonaws.com"
        },
        "pathRewrite": {
            "^/api/profile": "/dev/profile"
        }
    },
}

I think there is an issue with the host header.

If I hit this now, I get unauthorized

在此处输入图片说明

However when I add the host header explicitly in postman, it works.

在此处输入图片说明

I used the bypass option.

To use it we have to change the proxy.conf.json to proxy.conf.js . Check that all the references to proxy.conf.json now point to proxy.conf.js ( maybe you have it in angular.json , in options.proxyConfig or in the package.json ).

After this we add the code to add the bypass option(in proxy.conf.js and it would end looking something like this:

const PROXY_CONFIG =
{
    "/api/profile/*": {
        target: "http://asdasdfsdf.execute-api.ap-southeast-2.amazonaws.com",
        secure: false,
        logLevel: "debug",
        changeOrigin: false,
        bypass: function (req, res, proxyOptions) {
            req.headers["host"] = "asdasdfsdf.execute-api.ap-southeast-2.amazonaws.com";
        },
        pathRewrite: {
            "^/api/profile": "/dev/profile"
        }
    },
};

module.exports = PROXY_CONFIG;

"target": "http://asdasdfsdf.execute-api.ap-southeast-2.amazonaws.com" was doing a redirect to https:// ... . The proxy respected the redirect but it did not forward the headers in the config.

The solution was to use the https directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM