Skip to content

S3 Static Website: HTML files served as text/plain despite correct Content-Type metadata - tried multiple solutions

0

I'm hosting a static website on S3 and having persistent issues with HTML files being served as text/plain instead of text/html, causing browsers to display raw HTML code instead of rendering the webpage.

My Setup:

The Problem: When accessing the website, browsers show raw HTML code instead of rendering the page. Browser developer tools show Content-Type: text/plain instead of text/html.

What I've Tried:

  1. Metadata Setting During Upload:

    • Set Content-Type to text/html in S3 console during file upload
    • Used both "System defined" and "User defined" metadata options
    • Verified metadata shows Content-Type: text/html in object properties
  2. Multiple Upload Methods:

    • Uploaded via S3 console drag-and-drop
    • Tried uploading with different browsers
    • Re-uploaded files multiple times with metadata
  3. File Verification:

    • HTML file is valid and renders correctly locally
    • File extension is .html
    • No special characters in filename
  4. Bucket Configuration:

    • Confirmed static website hosting is enabled
    • Using correct website endpoint (not REST API endpoint)
    • Bucket policy allows s3:GetObject for all objects
  5. Cache Clearing:

    • Tested in incognito/private browsing
    • Cleared browser cache
    • Tried different browsers

Current Status:

  • Object metadata correctly shows Content-Type: text/html
  • Bucket policy allows public access
  • Static website hosting is properly configured
  • But HTTP response still returns Content-Type: text/plain

Questions:

  1. Why is S3 ignoring the Content-Type metadata I've set?
  2. Are there any S3 static website hosting limitations with Content-Type headers?
  3. What else can I check or try to resolve this issue?

Any help would be greatly appreciated!

asked 10 months ago418 views

2 Answers
0
Accepted Answer

It's not the bucket settings or file type, they all look good. You can prove this by viewing the index.html file locally in a browser and it will look exactly the same as the version that's being served from the S3 bucket.

The problem is the content of the index.html file itself.

$ curl http://hduaneblack.com.s3-website.us-east-2.amazonaws.com/index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta http-equiv="Content-Style-Type" content="text/css">
  <title></title>
  <meta name="Generator" content="Cocoa HTML Writer">
  <meta name="CocoaVersion" content="2685.2">
  <style type="text/css">
    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
  </style>
</head>
<body>
<p class="p1">&lt;!DOCTYPE html&gt;</p>
<p class="p1">&lt;html&gt;</p>
<p class="p1"><span class="Apple-converted-space">  </span>&lt;head&gt;</p>
<p class="p1"><span class="Apple-converted-space">    </span>&lt;title&gt;H. Duane Black&lt;/title&gt;</p>
<p class="p1"><span class="Apple-converted-space">  </span>&lt;/head&gt;</p>
<p class="p1"><span class="Apple-converted-space">  </span>&lt;body&gt;</p>
<p class="p1"><span class="Apple-converted-space">    </span>&lt;h1&gt;Welcome to my AWS site&lt;/h1&gt;</p>
<p class="p1"><span class="Apple-converted-space">    </span>&lt;p&gt;This is a private test site while I build my author website.&lt;/p&gt;</p>
<p class="p1"><span class="Apple-converted-space">  </span>&lt;/body&gt;</p>
<p class="p1">&lt;/html&gt;</p>
</body>
</html>

The rows between <body> and </body> are basically telling the browser to display the text that follows to look like rows of raw HTML, and not to be like what that HTML would look would look like when rendered (if that makes sense).

Simplify the index.html to look like:

<!DOCTYPE html>
<html>
  <head>
    <title>H. Duane Black</title>
  </head>
  <body>
    <h1>Welcome to my AWS site</h1>
    <p>This is a private test site while I build my author website.</p>
  </body>
</html>

Then upload that to the bucket, set the permissions & filetype etc., and view it in a browser now.

EXPERT

answered 10 months ago

EXPERT

reviewed 10 months ago

EXPERT

reviewed 10 months ago

0

This issue with HTML files being served as text/plain despite having the correct Content-Type metadata is a common problem with S3 static website hosting. Here are some potential causes and solutions:

  1. Website Endpoint vs. S3 Object URL: Make sure you're accessing your website through the S3 website endpoint (http://hduaneblack.com.s3-website.us-east-2.amazonaws.com) and not the regular S3 object URL. The regular S3 object URL might ignore the Content-Type metadata.

  2. Bucket Policy Configuration: While you mentioned your bucket policy allows public read access, ensure it specifically includes the s3:GetObject permission for all objects in your bucket. Your policy should look similar to:

{
"Version": "2012-10-17",
"Statement": [{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:GetObject"],
"Resource": ["arn:aws:s3:::hduaneblack.com/*"]
}]
}
  1. Block Public Access Settings: Check that "Block all public access" is turned off at the bucket level, as this could override your bucket policy.

  2. Content-Type Setting Method: When uploading files, try explicitly setting the Content-Type using the AWS CLI instead of the console:

aws s3 cp index.html s3://hduaneblack.com/ --content-type "text/html"
  1. File Permissions: Ensure the files have the correct permissions set to be publicly readable.

  2. CloudFront Configuration: If you're using CloudFront, check that it's not overriding your Content-Type settings.

  3. Try AWS Amplify Hosting: If you continue to face issues, consider using AWS Amplify Hosting, which can simplify the process of hosting static websites and handle many of these configuration details automatically.

If you've verified all these points and are still experiencing issues, you might want to try creating a new bucket with a different name to see if the problem persists, which could help identify if there's a specific issue with your current bucket configuration.
Sources
Rendering problem of the static website hosting | AWS re:Post
Use Amazon S3 website hosting to host without a single web server - Hosting Static Websites on AWS

answered 10 months ago

EXPERT

reviewed 10 months 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.