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
asked a year ago2952 views
1 Answer
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
EXPERT
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions