【问题标题】:Implementation of Reverse Proxy Server in AWS Lambda functionAWS Lambda 函数中反向代理服务器的实现
【发布时间】:2020-08-19 11:22:48
【问题描述】:
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);

这是我正在使用的代码。我的要求是实现反向代理服务器。问题是我在浏览器中收到以下错误:

GET http://127.0.0.1:3000/net::ERR_CONTENT_DECODING_FAILED 200(正常)

服务器正在正确发送响应,但压缩存在问题。

我尝试将以下 Mime 类型作为第三个参数传递给 createServer 函数:

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);

但是没有运气。

谁能帮我解决这个问题?提前致谢。

【问题讨论】:

    标签: node.js amazon-web-services express aws-lambda reverse-proxy


    【解决方案1】:

    我通过覆盖 Accept-Encoding 标头解决了这个问题:

    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);
    });
    

    此解决方案未优化。由于此解决方案将发送未压缩的响应,这将增加响应内容的长度。但是现在这解决了我的问题。如果我找到任何其他解决方案,我会更新。

    【讨论】:

      猜你喜欢
      • 2018-04-22
      • 2021-04-25
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2018-08-23
      • 2018-02-16
      • 2017-08-16
      相关资源
      最近更新 更多