Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve integration with cloud static website hosting #12607

Closed
depp opened this issue Jun 18, 2024 · 2 comments · Fixed by #12608
Closed

Improve integration with cloud static website hosting #12607

depp opened this issue Jun 18, 2024 · 2 comments · Fixed by #12608
Milestone

Comments

@depp
Copy link
Contributor

depp commented Jun 18, 2024

Proposal: Add an option to remap files named <dir>/index.html to <dir>/ during deployment.

Background

Traditional web servers (e.g. Apache, nginx) are typically configured to serve index.html files when users visit a directory. However, blob storage systems (e.g. S3, GCS) let you create files with names ending in /, and this is sometimes preferred.

For example, let’s say you deploy your site to an S3 bucket and CloudFront distribution. The simplest way to do this is to just use an S3 origin for your CloudFront distribution. However, this simple setup doesn’t work with Hugo out of the box—your website root / will correctly map to the root index.html file, but subdirectories will not work. You will get a 404 error for dir/, because CloudFront will look for an S3 object named dir/ instead of dir/index.html.

There are some workarounds for this:

  • You can enable “static website hosting” for your S3 bucket, set the index document to index.html, and configure CloudFront to use an HTTP origin that points to the S3 bucket. However, this prevents you from using the normal authentication & authorization systems that you would otherwise get, so this option only works for sites that are public.

  • You can use a CloudFront function to remap requests to dir/ to request dir/index.html, but this adds complexity and code to what would otherwise be a no-code configuration. See: https://stackoverflow.com/a/76581267

Solution

Add a new option to the deployment.targets section of the configuration called stripIndexHTML. This option maps any local file named <dir>/index.html to a remote file named <dir>/. The root page, index.html, is untouched.

This is “elegant” because this just means that the remote file path matches the canonical URLs that Hugo uses anyways. Your link to dir/ maps to a remote file named dir/.

Impact

From a web search, I can see that other people using Hugo have experienced this problem:

  • discourse.gohugo.io: “Index.html pages in S3 bucket aren’t loading through CloudFront distribution”
  • Stack Overflow: “Hugo on S3 with CloudFront doesn't redirect to index.html.”
  • Stack Overflow: “Hugo multilingual site deploy to aws s3 error ‘AccessDenied’”
  • Stack Overflow: “AWS static links fail without ‘index.html’”
depp added a commit to depp/hugo that referenced this issue Jun 18, 2024
This new configuration parameter causes paths matching
"<dir>/index.html" to be stored as "<dir>/" remotely. This simplifies
the cloud configuration needed for some use cases, such as CloudFront
distributions with S3 bucket origins. Before this change, users must
configure their S3 buckets as public websites (which is incompatible
with certain authentication / authorization schemes), or users must add
a CloudFormation function to add index.html to the end of incoming
requests. After this change, users can simply use an ordinary CloudFront
distribution (no additional code) with an ordinary S3 bucket origin (and
not an S3 website).

This adds tests to ensure that functionality like matchers is unaffected
by this change. I have also tested that the functionality works as
expected when deploying to a real S3 / CloudFront website.

Closes gohugoio#12607
@bep bep removed the NeedsTriage label Jun 19, 2024
@bep bep added this to the v0.128.0 milestone Jun 19, 2024
@bep
Copy link
Member

bep commented Jun 19, 2024

@vangent ... does the above match your world view?

@vangent
Copy link
Contributor

vangent commented Jun 19, 2024

I do not have any expertise in web site hosting, but the explanation sounds plausible and the proposed solution doesn't sound too hard.

@bep bep closed this as completed in d5542ed Jun 20, 2024
@bep bep added Enhancement and removed Proposal labels Jun 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants