12
votes

My setup is as follows:

  1. user types example.com on the browser
  2. request goes to AWS CloudFront, which redirects HTTP to HTTPS, and forwards the request to the AWS Elastic LoadBalancer (elb.example.com)
  3. LoadBalancer forwards the request to the EC2 instance running PHP Laravel framework
  4. EC2 responds normally
  5. user views the page correctly at example.com with everything else transparent to him

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

  • If the user navigates to any button on the page, the url on the browser will become elb.example.com (it should stay example.com)
  • If I go to view page source, all the links to any button on the page has the base url of elb.example.com (it should be 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.

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

2
Can you paste what is output of .htaccess? Also did you check the DB has URL "example.com" and not elb.example.com - error2007s
.htaccess is a file, do you want me to share the file ? Please explain what do you mean by DB ? Do you mean database ? Cause database has nothing to do with this issue in my opinion - Dv_MH
Ok Forget about the hatches and DB where is your website domain pointing ? is it using a C-name or an IP address? - error2007s
Or I can trouble shoot the issue more fast if you give me the URL of your website. - error2007s
I have an alias for my domain example.com pointing to d123456.cloudfront.net and an alias elb.example.com pointing to the load balancer - Dv_MH

2 Answers

19
votes

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.

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.

3
votes

In the meantime things have changed a bit (on the AWS side) from the original answer. As it is still correct in it's core. (Adding the host header to the whitelist.) Now you need to configure this as a custom Cloudfront policy. You need to create this policy in the Cloudfront AWS console.

After you created the custom policy, you can configure this newly created policy using the 'behaviours' tab of your distribution. This can be done by changing the 'cache policy' to your newly created policy.