How to make CloudFront forward the Host or X-Forwarded-Host header to API Gateway

2

My API implementation needs to read the Host header sent by the browser as part of its behaviour. However it seems that the Host header is overwritten by cloudfront when it is calling the API Gateway Origin.

This post suggest that it is possible to configure the Host header to be forwarded, however doing so does not work, because the API gateway is no longer being called correctly afterwards (API gateway seems to depend on the value of this header).

I would like to be able to configure this in CloudFront without using lambda@edge or CloudFront functions.

Ruud
질문됨 일 년 전3037회 조회
1개 답변
0

AWS CloudFront, by default, does not forward the Host header to the origin because it uses this header to route requests to the correct location. This is why your API Gateway is failing when you try to forward this header -- it relies on the Host header to determine where to send the request. you cannot directly forward the Host header or X-Forwarded-Host header to the API Gateway using CloudFront without using Lambda@Edge or CloudFront Functions. These services allow you to modify the headers between the client and CloudFront or between CloudFront and the origin server. If your API needs the original Host header to function correctly, and you can't use Lambda@Edge or CloudFront Functions, you might need to consider a different design approach. Instead of relying on the Host header, you could use a custom header to pass the necessary information. You would set this header in your client application and configure CloudFront to forward it to your origin. Depending on the nature of your API and the information you're trying to pass, it might be possible to include it in the request body (for POST requests) or the URL (as a query string or part of the path).

profile picture
전문가
답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠