I want to remove or modify HTTP response headers such as Server, X-Cache, X-Forwarded-Host, or X-Forwarded-Server from my Amazon CloudFront distribution.
Short description
Choose one of the following resolutions based on the response header that you want to modify or remove:
- To unconditionally modify or remove the value of a Server header, use a response headers policy.
- To modify the value of a Server header for a specific condition, use a CloudFront function.
- To remove an X-Forwarded-Host or X-Forwarded-Server header, use custom headers.
- You can't remove or modify the X-Cache header. The X-Cache header must remain because it shows the header's source, such as the CloudFront cache, a function, or an origin.
Note: CloudFront adds its own Server and X-Cache headers to responses that it sends to viewers. CloudFront adds the headers even when you use a response headers policy to remove the headers from the origin response.
Resolution
Use a response headers policy to modify or remove a Server header
To modify a Server header, complete the following steps:
- Open the CloudFront console.
- In the navigation pane, choose Policies, and then choose the Response headers tab.
- Choose Create response headers policy.
- In the Details section, for Name, enter a name for your policy.
- In the Custom headers section, choose Add header.
- For Name, enter Server.
- For Value, enter your Server header value.
- Select the Origin override option for your custom Server header, and then choose Create.
- Attach the response headers policy to the behavior that you want in your CloudFront distribution.
CloudFront deploys the changes and then overrides the Server header value with the custom value that you specified in the policy.
To remove a Server header, complete the following steps:
- Open the CloudFront console.
- In the navigation pane, choose Policies, and then choose the Response headers tab.
- Choose Create response headers policy.
- In the Details section, for Name, enter a name for your policy.
- In the Remove headers section, choose Add header.
- For Name, enter Server, and then choose Create.
- Attach the response headers policy to the behavior that you want in your CloudFront distribution.
CloudFront deploys the changes and then replaces the Server header value with the CloudFront value.
Use a CloudFront Function to modify the Server header for a specific condition
To override the Server header value with your custom value for the behavior that you specify for a specific condition, complete the following steps:
- Open the CloudFront console.
- In the navigation pane, choose Functions, and then choose Create function.
- For Name, enter a name for your function.
- For Runtime, choose cloudfront-js-2.0.
- Choose Create function.
- In the Development tab of the code editor, enter the following code:
function handler(event) {
var response = event.response;
var headers = response.headers;
if ('server' in headers && headers.server.value === 'YOUR_CONDITIONAL_VALUE') {
headers.server.value = 'YOUR_CUSTOM_VALUE';
}
return response;
}
Note: Replace YOUR_CONDITIONAL_VALUE with your conditional value and YOUR_CUSTOM_VALUE with your Server header value.
- Choose Save changes.
- Associate the function with the behavior that you want in your CloudFront distribution.
The CloudFront Function overrides the Server header with your custom value for the behavior in the specific condition that you defined in the function.
Use custom headers to remove the "X-Forwarded-Host" or "X-Forwarded-Server" header
Complete the following steps:
-
Create a Lambda@Edge function.
-
Add a trigger for the Lambda@Edge function.
The following example function in Node.js removes the X-Forwarded-Host and X-Forwarded-Server headers:
export const handler = async (event) => {
const response = event.Records[0].cf.response;
const headers = response.headers;
// Remove X-Forwarded-Host header
delete headers['x-forwarded-host'];
// Remove X-Forwarded-Server header
delete headers['x-forwarded-server'];
return response;
};
-
Deploy the Lambda@Edge function, and then associate the function with your CloudFront distribution's viewer request trigger.
-
Use the trigger to remove the specified headers before you forward requests to the origin.
Related information
Understand response headers policies
Customize at the edge with CloudFront Functions
Customize at the edge with Lambda@Edge