Custom Error Pages and Responses for Amazon CloudFront
Amazon CloudFront distributes dynamic and static web content produced by an origin server to viewers located anywhere in the world. If the user requests objects that don’t exist (i.e., a 404 Not Found response) or an unauthorized user might attempt to download an object (i.e., a 403 Forbidden response), CloudFront used to display a brief and sparsely formatted error message:
Today we are improving CloudFront, giving you the ability to control what’s displayed when an error is generated in response to your viewer’s request for content. You can have a distinct response for each of the supported HTTP status codes.
The CloudFront Management Console contains a new tab for Error Responses:
Click on the Create Custom Error Response button to get started, then create the error response using the following form:
You can create a separate custom error response for each of the ten HTTP status codes listed in the menu. The Response Page Path points to the page to be returned to signify the response. For best results, point this to an object in an Amazon S3 bucket. This will prove to be more reliable than storing the pages on the origin server in the event that the server returns any of the 5xx status codes.
You can also choose the HTTP status code that will be returned along with the response page (in most cases you’ll want to use 200):
Finally, you can set the Error Caching Time To Live (TTL) for the error response. By default, CloudFront will cache the response to 4xx and 5xx errors for five minutes. You can change this value as desired. Note that a small value will cause CloudFront to forward more requests to the origin server; this may increase the load on the server and cause further issues.
Your origin server can also control the TTL by returning Cache-Control or Expires headers as part of the error response.