Writing

Web Development · 3 min read

Setting Up CORS Vary-Origin Headers in AWS S3

The exact S3 CORS configuration I used to make responses vary by origin instead of letting a CDN cache the wrong header.

In this article3 sections

I needed one S3 bucket to serve assets to multiple domains. The request worked for one origin and then failed for another because the cached response carried the first site’s CORS header. The missing piece was Vary: Origin.

Say you have assets in S3 that example.com and another.com both use. S3 returns Access-Control-Allow-Origin: https://example.com to the first request. A CDN caches that response. When another.com requests the same asset, it gets the cached response with the wrong origin, and the browser blocks it. Vary: Origin tells caches: “This response changes based on the Origin header. Don’t reuse it for different origins.”

The configuration

These two CORS rules produced Vary: Origin in my 2016 S3 setup:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <CORSRule>
    <AllowedOrigin>http*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedHeader>*</AllowedHeader>
  </CORSRule>
  <CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>HEAD</AllowedMethod>
  </CORSRule>
</CORSConfiguration>

This is the XML configuration from my 2016 setup. The current S3 console requires JSON; do not paste this XML into it. The first rule matches http:// and https:// origins and returns the specific origin in Access-Control-Allow-Origin. The second handles simple GET and HEAD requests with * as a fallback. Those were the responses I recorded in that setup; verify your actual GET, HEAD, and preflight behavior rather than relying on this historical combination.

Testing the actual request path

My old example sent HEAD with an Access-Control-Request-Method: GET header. That is not a browser preflight, and its response should not be used to predict an actual GET or OPTIONS response.

For a bucket and object you control, inspect a GET response:

curl -sS -D - -o /dev/null \
  -H "Origin: https://example.com" \
  https://example-bucket.s3.amazonaws.com/sample.xml

If the browser request needs a preflight, test OPTIONS with the actual requested method and headers:

curl -sS -D - -o /dev/null -X OPTIONS \
  -H "Origin: https://example.com" \
  -H "Access-Control-Request-Method: GET" \
  -H "Access-Control-Request-Headers: X-Example" \
  https://example-bucket.s3.amazonaws.com/sample.xml

X-Example is only a placeholder: omit it if the real request has no such header. S3 evaluates the first matching CORS rule using the requested origin, method, and headers. Repeat the check with another allowed origin and an unlisted one. Then test through the CDN and in a browser; curl displays headers but does not enforce CORS.

The CDN cache needs its own configuration

Vary: Origin describes a response variant, but it does not configure every CDN’s cache key. CloudFront’s documentation requires explicit header-forwarding and caching settings for CORS. Forward Origin; when caching OPTIONS responses, account for Access-Control-Request-Method and Access-Control-Request-Headers too. Check both the origin request policy and cache policy for the actual distribution.

The two-rule XML above records an old experiment, not a requirement for every bucket. Use explicit allowed origins when limiting browser access to particular sites. A response deliberately available to any origin can use Access-Control-Allow-Origin: * when the request’s credentials mode is not include; that origin header does not need a separate variant per site.

For a deeper dive into how CORS works and how to configure it in Apache and nginx, see my post on understanding CORS basics.

In 2025, I moved away from S3 to Cloudflare Workers for static hosting, which handles CORS configuration more simply and deploys to the edge globally.

Was this useful?

What was missing?

Thanks for the feedback.