简体   繁体   中英

Implementation of Reverse Proxy Server in AWS Lambda function

const app = require('express')();
const { createProxyMiddleware } = require('http-proxy-middleware');

    app.all('*', createProxyMiddleware({ target: 'https://www.google.com/', changeOrigin: true 
}));

const awsServerlessExpress = require('aws-serverless-express');
const server = awsServerlessExpress.createServer(app);

exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context);

This is the code I am using. My requirement is to implement Reverse Proxy Server. The issue is I am getting following error in Browser:

GET http://127.0.0.1:3000/ net:: ERR_CONTENT_DECODING_FAILED 200 (OK)

Server is sending the response properly but there is an issue with Compression.

I tried passing following Mime-types as a 3rd Parameter to createServer function:

const binaryMimeTypes = [
    'application/javascript',
    'application/json',
    'application/octet-stream',
    'application/xml',
    'font/eot',
    'font/opentype',
    'font/otf',
    'image/jpeg',
    'image/png',
    'image/svg+xml',
    'text/comma-separated-values',
    'text/css',
    'text/html',
    'text/javascript',
    'text/plain',
    'text/text',
    'text/xml'
]

const server = awsServerlessExpress.createServer(app, null, binaryMimeTypes);

But had no luck.

Can anyone please help me out with this? Thanks in advance.

I solved it by overriding the Accept-Encoding header:

app.all('*', (req, res, next) => {
    createProxyMiddleware({ target: 'https://www.google.com/', changeOrigin: true,
        onProxyReq: (proxyReq, req, res) => {
            proxyReq.setHeader('Accept-Encoding', 'identity');
        }
    })(req, res, next);
});

This solution is not optimized. As this solution will send response un-compressed which will increase the response content length. But for now this solved my issue. I will update if I find any other solution.

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