【发布时间】:2019-12-09 10:40:33
【问题描述】:
我有一系列在 AWS 上运行的无服务器 next.js 应用程序,我在子域中提供服务,我想将它们代理到我的主域上的子目录。因此,例如,foo.example.com/foo/ 的应用应该出现在www.example.com/foo/。
我已经通过使用 http-proxy 和 express 实现了这一点。我有一个相当简单的快速服务器,它在自己的无服务器应用程序中运行,如下所示:
const serverless = require('serverless-http');
const httpProxy = require('http-proxy');
const express = require('express');
const app = express();
const proxy = httpProxy.createProxyServer();
app.get('/', (req, res) => {
res.send('Hello, you are at index.');
});
app.get('/:project/*', (req, res) => {
const project = req.params.project;
const rest = req.params[0];
const url = `https://${project}.example.com/${project}/`;
req.url = `/${rest}`;
req.headers['X-Projects-Router-Proxy'] = true;
req.body = undefined;
proxy.web(req, res, {
target: url,
xfwd: false,
toProxy: true,
changeOrigin: true,
secure: true,
});
});
module.exports.handler = serverless(app);
这本身就可以很好地工作,这很棒。但是,当我尝试将其放在 CloudFront 后面时,索引页面可以正常工作,但任何触及代理的内容都会返回 403 错误:
403 ERROR
The request could not be satisfied.
Bad request.
Generated by cloudfront (CloudFront)
这里可能出了什么问题,如何配置 http-proxy 以使其与 CloudFront 合作?
【问题讨论】:
标签: proxy amazon-cloudfront serverless-framework node-http-proxy