AWS Cloudfront + Load Balancer, url changes from main domain to load balancer subdomain
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.
cloudfront domain replaced by application load balancer dns name when redirecting from http to https
Okay we've resolved this. Firstly, we were not bypassing the Host header. So to get it working we set 'Cache Based on Selected Request Headers' to whitelist & whitelist Host header. (To know more see this answer AWS Cloudfront + Load Balancer, url changes from main domain to load balancer subdomain)
Now when host is set correctly, we were either hitting the infinite redirect loop or wrong certificate error. to get out of this we changed how CDN (cloudfront) is establising connection with our load balancer. Previously it was only by HTTP to avoid any problems with certificates. But now it's impossible, because we're redirecting from HTTP and it'd create an infinite redirection loop. So we configured HTTPS
in CDN <-> ALB connection(updated Origin Protocol Policy to HTTPS
). And in order to bypass problems with certificates, changed xxxxx.cloudfront.net
domain to the cdn.mysite.com
(using CNAME in route53 configurations) and added our custom certificate used for *.mysite.com.
Paths won't resolve in domain name but in Elastic Beanstalk URL
So, I found a solution for my problem. By default, CloudFront sets HTTP Request Header to the origin hostname -- for me this was the ElasticBeanstalk URL. The application then generates links based on that origin.
To change that behavior one has to create a Cache Policy and an Origin Request Policy and add the host header to a whitelist, so CloudFront uses the hist header that is sent from the browser.
This answer gave me a hint on what to do, as well as these AWS resources:
- This gives an overview about the solution
- How to create a Cache Policy and attach it to your distribution
- How to create an Origin Request Policy and attach it to your distribution
How to point a domain *subdirectory* (not subdomain) to AWS EC2 Instance
For this behaviour you cannot use just Route 53 as subfolders are not part of DNS resolution.
There are many solutions you could use but the most common for a website are using either CloudFront or an ALB to use path based routing.
CloudFront
With this solution you would put CloudFront in front of your domain (Route 53 will route to a CloudFront distribution.
A forwarded location is known as an origin, therefore you would create an origin for each of the target hosts.
The routing mechanism within CloudFront would be the behaviour, with a default behaviour used to forward to your default host.
You would then create an additional behaviour based on the path pattern of the domain that you would want to be routed to the other origin (e.g. /blog*).
More information about this option is available in the Can I use a single CloudFront web distribution to serve content from multiple origins using multiple behaviors? document.
Application Load Balancer
With this option your Route 53 record would forward to an ALB.
You would create a target group for each EC2 instance, and then register this hosts within the target group.
Within the listener in the ALB you would forward all traffic by default to one fo your target groups, then add an additional rule to the listener based on path patterns to forward to the other target group when appropriate.
More information is available within the Advanced Request Routing for AWS Application Load Balancers blog post.
Related Topics
Php: Get N-Th Item of an Associative Array
How to Pass Parameters by Reference Using Call_User_Func_Array()
How to Get Primary Key of Table
Continue Processing After Closing Connection
How to Properly Use While Loop in Pdo Fetchall
Accessing Variables and Methods Outside of Class Definitions
Access a Global Variable in a PHP Function
How to Make Strings "Xml Safe"
Difference Between & and && in PHP
Php's Preg_Match() and Preg_Match_All() Functions
How to Test If a User Has Selected a File to Upload
Codeigniter Csrf - How Does It Work
Regex for Checking If a String Has Mismatched Parentheses
How to Remove "Via" and Server Name When Sending Mails with PHP