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

查看:238
本文介绍了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 .....

  • 如果用户导航到页面上的任何按钮,则浏览器上的网址将变为elb.example.com(应保留为example.com)
  • 如果我要查看页面源代码,则该页面上任何按钮的所有链接的基本网址均为elb.example.com(应该为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看到基本网址为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查找,以建立到源的TCP连接(在本例中为ELB),但停止将该主机名注入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天全站免登陆