【问题标题】:http-proxy to cloudfront results in 403 errorhttp-proxy 到 cloudfront 导致 403 错误
【发布时间】: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


    【解决方案1】:

    您需要将 *.example.com 添加到 CloudFront CNAME/备用名称字段以及 DNS 中以将其指向 CloudFront,当 url 更改为 {project}.example.com 时,CloudFront 会根据主机标头查找分配如果找不到,它会给你 403 错误。

    【讨论】:

    • 不幸的是,这些域都已在 CNAME/备用名称字段中列出。我不确定您对主机标头的意思是什么——CloudFront 在那里期望什么?
    • cloudfront 期望请求中的主机标头应与添加到其中的备用名称匹配。
    猜你喜欢
    • 2019-09-08
    • 2015-03-31
    • 2012-01-30
    • 2015-09-25
    • 2018-01-08
    • 2015-07-17
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多