AWS Cloudfront + Load Balancer, url changes from main domain to load balancer subdomain












3














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 ?










share|improve this question






















  • Can you paste what is output of .htaccess? Also did you check the DB has URL "example.com" and not elb.example.com
    – error2007s
    Jul 9 '16 at 23:06










  • .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
    Jul 9 '16 at 23:18










  • Ok Forget about the hatches and DB where is your website domain pointing ? is it using a C-name or an IP address?
    – error2007s
    Jul 9 '16 at 23:36










  • Or I can trouble shoot the issue more fast if you give me the URL of your website.
    – error2007s
    Jul 9 '16 at 23:38










  • 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
    Jul 9 '16 at 23:51
















3














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 ?










share|improve this question






















  • Can you paste what is output of .htaccess? Also did you check the DB has URL "example.com" and not elb.example.com
    – error2007s
    Jul 9 '16 at 23:06










  • .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
    Jul 9 '16 at 23:18










  • Ok Forget about the hatches and DB where is your website domain pointing ? is it using a C-name or an IP address?
    – error2007s
    Jul 9 '16 at 23:36










  • Or I can trouble shoot the issue more fast if you give me the URL of your website.
    – error2007s
    Jul 9 '16 at 23:38










  • 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
    Jul 9 '16 at 23:51














3












3








3







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 ?










share|improve this question













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 ?







php .htaccess amazon-web-services amazon-ec2 amazon-cloudfront






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jul 9 '16 at 22:32









Dv_MH

51712059




51712059












  • Can you paste what is output of .htaccess? Also did you check the DB has URL "example.com" and not elb.example.com
    – error2007s
    Jul 9 '16 at 23:06










  • .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
    Jul 9 '16 at 23:18










  • Ok Forget about the hatches and DB where is your website domain pointing ? is it using a C-name or an IP address?
    – error2007s
    Jul 9 '16 at 23:36










  • Or I can trouble shoot the issue more fast if you give me the URL of your website.
    – error2007s
    Jul 9 '16 at 23:38










  • 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
    Jul 9 '16 at 23:51


















  • Can you paste what is output of .htaccess? Also did you check the DB has URL "example.com" and not elb.example.com
    – error2007s
    Jul 9 '16 at 23:06










  • .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
    Jul 9 '16 at 23:18










  • Ok Forget about the hatches and DB where is your website domain pointing ? is it using a C-name or an IP address?
    – error2007s
    Jul 9 '16 at 23:36










  • Or I can trouble shoot the issue more fast if you give me the URL of your website.
    – error2007s
    Jul 9 '16 at 23:38










  • 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
    Jul 9 '16 at 23:51
















Can you paste what is output of .htaccess? Also did you check the DB has URL "example.com" and not elb.example.com
– error2007s
Jul 9 '16 at 23:06




Can you paste what is output of .htaccess? Also did you check the DB has URL "example.com" and not elb.example.com
– error2007s
Jul 9 '16 at 23:06












.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
Jul 9 '16 at 23:18




.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
Jul 9 '16 at 23:18












Ok Forget about the hatches and DB where is your website domain pointing ? is it using a C-name or an IP address?
– error2007s
Jul 9 '16 at 23:36




Ok Forget about the hatches and DB where is your website domain pointing ? is it using a C-name or an IP address?
– error2007s
Jul 9 '16 at 23:36












Or I can trouble shoot the issue more fast if you give me the URL of your website.
– error2007s
Jul 9 '16 at 23:38




Or I can trouble shoot the issue more fast if you give me the URL of your website.
– error2007s
Jul 9 '16 at 23:38












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
Jul 9 '16 at 23:51




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
Jul 9 '16 at 23:51












1 Answer
1






active

oldest

votes


















10














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.






share|improve this answer























  • Thanks for the great answer. I'm trying this now and will update you once I see results
    – Dv_MH
    Jul 10 '16 at 9:52






  • 1




    Worked perfect. Thanks a lot for the very informative answer.
    – Dv_MH
    Jul 10 '16 at 10:18











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38286901%2faws-cloudfront-load-balancer-url-changes-from-main-domain-to-load-balancer-su%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









10














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.






share|improve this answer























  • Thanks for the great answer. I'm trying this now and will update you once I see results
    – Dv_MH
    Jul 10 '16 at 9:52






  • 1




    Worked perfect. Thanks a lot for the very informative answer.
    – Dv_MH
    Jul 10 '16 at 10:18
















10














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.






share|improve this answer























  • Thanks for the great answer. I'm trying this now and will update you once I see results
    – Dv_MH
    Jul 10 '16 at 9:52






  • 1




    Worked perfect. Thanks a lot for the very informative answer.
    – Dv_MH
    Jul 10 '16 at 10:18














10












10








10






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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 12:30

























answered Jul 10 '16 at 4:46









Michael - sqlbot

87.8k12127190




87.8k12127190












  • Thanks for the great answer. I'm trying this now and will update you once I see results
    – Dv_MH
    Jul 10 '16 at 9:52






  • 1




    Worked perfect. Thanks a lot for the very informative answer.
    – Dv_MH
    Jul 10 '16 at 10:18


















  • Thanks for the great answer. I'm trying this now and will update you once I see results
    – Dv_MH
    Jul 10 '16 at 9:52






  • 1




    Worked perfect. Thanks a lot for the very informative answer.
    – Dv_MH
    Jul 10 '16 at 10:18
















Thanks for the great answer. I'm trying this now and will update you once I see results
– Dv_MH
Jul 10 '16 at 9:52




Thanks for the great answer. I'm trying this now and will update you once I see results
– Dv_MH
Jul 10 '16 at 9:52




1




1




Worked perfect. Thanks a lot for the very informative answer.
– Dv_MH
Jul 10 '16 at 10:18




Worked perfect. Thanks a lot for the very informative answer.
– Dv_MH
Jul 10 '16 at 10:18


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38286901%2faws-cloudfront-load-balancer-url-changes-from-main-domain-to-load-balancer-su%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)