【问题标题】:Use gurnt-connect-proxy proxy to cloudfront get 403 error使用 gurnt-connect-proxy 代理到 cloudfront 得到 403 错误
【发布时间】:2019-09-08 09:19:18
【问题描述】:

我使用grunt-connect-proxy 将某些页面反向代理到我的 CloudFront 网站,但它无法正常工作。

环境:

  • 咕哝:^0.4.5
  • grunt-contrib-connect: ^0.9.0,
  • grunt-connect-proxy:^0.2.0

我尝试了什么

  1. 使用http-proxy(成功):
httpProxy.createProxyServer({target: {
  protocol: 'http:',
  host: 'myhostname.com',
  port: 80,
},
headers: {
  host: 'myhostname.com'
}}).listen(8080)
  1. 使用curl成功):
curl -v http://myhostname.com/signin
  1. 使用Nginx成功):
location ~* ^/(signin|styles|scripts) {
  proxy_pass http://myhostname.com;
}

使用grunt-connect-proxy失败

connect: {
  server: {
    options: {
      open: true,
        base: ['app'],
          middleware: function(connect, options, middlewares) {
            if (!Array.isArray(options.base)) {
              options.base = [options.base];
            }
            middlewares.push(modRewrite([
              '^\\/customer-portal\\/((?!\\.).)*$ /index.html'
            ]));
            middlewares.push(connect.static(options.base[0]));
            middlewares.push(require('grunt-connect-proxy/lib/utils').proxyRequest)
            return middlewares;
          }
    },
    proxies: [{
      context: ['/signin', '/scripts', '/styles'],
      host: 'myhostname.com',
      port: '80',
      https: false,
    }]
  },
}

它从 CloudFront 获取 403 代码(我不确定 CloudFront 中的 403 是否为 404)。 如果我将headers 添加到proxies

proxies: [{
 ...
  headers: {
    'host': 'myhostname.com'
  }
 ...
}]

我从 chrome 收到 ERR_CONTENT_DECODING_FAILED 错误。 那么,正确的配置是什么?

【问题讨论】:

    标签: javascript proxy gruntjs amazon-cloudfront


    【解决方案1】:

    我通过http-proxy用一个客户中间件解决了这个问题,已经放弃使用grunt-connect-proxy,代码如下:

    middlewares.push(function(req, res, next) {
      if(someRegex.test(req.url) {
        proxy.web(req, res, {target: 'myhostname.com'})
      } else {
        return next();
      }
    })
    

    【讨论】:

      猜你喜欢
      • 2019-12-09
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      • 2018-04-25
      • 1970-01-01
      相关资源
      最近更新 更多