我有一系列运行在亚马逊网络服务上的无服务器next.js应用程序,我在子域中提供服务,我想将它们代理到我的主域上的子目录。例如,foo.example.com/foo/上的应用程序应该出现在www.example.com/foo/上。
我已经通过使用http-proxy和express实现了这一点。我有一个相当简单的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协作?
发布于 2019-08-01 10:02:17
你需要将*.example.com添加到CloudFront CNAME/Alternative name字段中,并在DNS中将其指向CloudFront,当url更改为{project}.example.com时,CloudFront会根据主机标头查找分发,如果找不到,则会给出403错误。
https://stackoverflow.com/questions/57298750
复制相似问题