首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >http-proxy to cloudfront导致403错误

http-proxy to cloudfront导致403错误
EN

Stack Overflow用户
提问于 2019-08-01 04:58:00
回答 1查看 1.2K关注 0票数 0

我有一系列运行在亚马逊网络服务上的无服务器next.js应用程序,我在子域中提供服务,我想将它们代理到我的主域上的子目录。例如,foo.example.com/foo/上的应用程序应该出现在www.example.com/foo/上。

我已经通过使用http-proxy和express实现了这一点。我有一个相当简单的express服务器,它在自己的无服务器应用程序中运行,如下所示:

代码语言:javascript
复制
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错误:

代码语言:javascript
复制
403 ERROR
The request could not be satisfied.
Bad request. 
Generated by cloudfront (CloudFront)

这里可能出了什么问题,我如何配置http-proxy以使其与CloudFront协作?

EN

回答 1

Stack Overflow用户

发布于 2019-08-01 10:02:17

你需要将*.example.com添加到CloudFront CNAME/Alternative name字段中,并在DNS中将其指向CloudFront,当url更改为{project}.example.com时,CloudFront会根据主机标头查找分发,如果找不到,则会给出403错误。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57298750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档