AWS Cloudfront + 负载均衡器,url 从主域更改为负载均衡器子域 [英] AWS Cloudfront + Load Balancer, url changes from main domain to load balancer subdomain

查看:49
本文介绍了AWS Cloudfront + 负载均衡器,url 从主域更改为负载均衡器子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设置如下:

  1. 用户在浏览器上输入 example.com
  2. 请求转到 AWS CloudFront,后者将 HTTP 重定向到 HTTPS,并将请求转发到 AWS Elastic LoadBalancer (elb.example.com)
  3. LoadBalancer 将请求转发到运行 PHP Laravel 框架的 EC2 实例
  4. EC2 正常响应
  5. 用户在 example.com 上正确查看页面,其他一切对他来说都是透明的

这一切都是我想要的,但是......

All this is perfectly what I want, HOWEVER .....

  • 如果用户导航到页面上的任何按钮,浏览器上的 url 将变为 elb.example.com(它应该保持 example.com)
  • 如果我去查看页面源代码,页面上任何按钮的所有链接都有 elb.example.com 的基本 URL(应该是 example.com)

原因是因为 EC2 看到来自负载均衡器的请求,所以它假定基本 url 是 elb.example.com 并相应地生成所有链接.

The reason is because EC2 see the request coming from the load balancer so it assumes the base url is elb.example.com and generates all links accordingly.

如何让 EC2 将基本 url 视为 example.com ?

How do make EC2 see the base url as example.com ?

推荐答案

这种行为可能是因为默认情况下 CloudFront 将 Host: HTTP 请求标头设置为原始主机名,在本例中elb.example.com.然后,应用程序可能会根据该主机名生成链接.

This behavior likely results from the fact that by default CloudFront sets the Host: HTTP request header to the origin hostname, in this case elb.example.com. The application then presumably generates links based on that hostname.

相反,如果您将 CloudFront 配置为将该标头列入白名单以转发到源,则浏览器 (example.com) 发送的 Host 标头将由 CloudFront 发送到应用程序,因此应用程序的行为应该更像您在生成链接时期望并使用该值.这样,CloudFront 仍然使用源域名进行 DNS 查找,以便建立到源(在本例中为 ELB)的 TCP 连接,但停止将该主机名注入 HTTP 请求标头中.

If, instead, you configure CloudFront to whitelist that header for forwarding to the origin, the Host header sent by the browser (example.com) will be sent on to the application by CloudFront, so the application should behave more like you'd expect and use that value when generating the links. With this, CloudFront still uses the origin domain name to do the DNS lookup needed in order to establish the TCP connection to the origin (the ELB in this case), but stops injecting that hostname into the HTTP request headers.

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-values-specify.html#DownloadDistValuesForwardHeaders

您将在缓存行为设置 -> 基于所选请求标头的缓存 -> 白名单下找到主机标头.

You'll find the host header under cache behavior settings -> cache based on selected request headers -> whitelist.

这篇关于AWS Cloudfront + 负载均衡器,url 从主域更改为负载均衡器子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆