7

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 ?

Mohamed Heiba
  • 241
  • 1
  • 2
  • 8
  • Can you just use relative links rather than outputting absolute URLs? – Joshua DeWald Jul 12 '16 at 15:34
  • This has been answered on StackOverflow [http://stackoverflow.com/questions/38286901/aws-cloudfront-load-balancer-url-changes-from-main-domain-to-load-balancer-su](http://stackoverflow.com/questions/38286901/aws-cloudfront-load-balancer-url-changes-from-main-domain-to-load-balancer-su) – Mohamed Heiba Jul 13 '16 at 15:01
  • Having just gone through this, all the other answers are either outdated, or way too vague to actually understand. Here's a walk-through: https://stackoverflow.com/questions/69399672 – Metro Smurf Oct 01 '21 at 21:42

1 Answers1

7

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 server hostname 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

This has been answered by @michael-sqlbot on SO

Sven
  • 97,248
  • 13
  • 177
  • 225
Mohamed Heiba
  • 241
  • 1
  • 2
  • 8
  • 1
    Under "Cache Behavior/Whitelist Headers" I whitelisted Host, Origin and Referrer and it worked. Probably you can try to whitelist only Host. – martins256 Dec 15 '17 at 12:55
  • Having just gone through this, all the other answers are either outdated, or way too vague to actually understand. Here's a walk-through: https://stackoverflow.com/questions/69399672 – Metro Smurf Oct 01 '21 at 21:42