You move static assets to a CDN subdomain and your custom fonts stop loading, with a console error saying the font “has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present”. To fix CDN font CORS errors in Nginx, you add that header to the font files at your origin, keep Nginx’s header inheritance from dropping it, and purge the copies your CDN already cached.
Quick answer: Add add_header Access-Control-Allow-Origin "*" always; inside an Nginx location block that matches your font extensions, reload Nginx, then purge the font URLs on your CDN. Browsers require CORS headers on cross-origin fonts, and the CDN keeps serving the old headerless copy until you purge it.
- Confirm the header is missing with
curl -I -H "Origin: https://example.com"on a font URL. - Add a font
locationblock withAccess-Control-Allow-Originand thealwaysflag. - Repeat any parent
add_headerlines in that block, or move them into an included snippet. - Run
sudo nginx -tand reload Nginx. - Purge
/fonts/*on the CDN and run the curl check again against the CDN URL.
In this guide
- Before You Start
- Why Web Fonts Break on CDNs (The Same-Origin Trap)
- Step 1: Confirm the Missing Header with cURL
- Step 2: Add Access-Control-Allow-Origin to the Font Location
- Step 3: Check for the add_header Inheritance Trap
- Step 4 (If Needed): Answer OPTIONS Preflight Requests
- Step 5: Purge the CDN Edge Cache and Verify
- If It Still Doesn’t Work
- Frequently Asked Questions
- Next Steps

Before You Start
- SSH access with
sudoto the origin server running Nginx. - Access to your CDN dashboard, so you can purge cached files.
- The URL of one font that fails, copied from the browser’s console or Network tab.
- About 15 minutes. Back up the site config before editing (
sudo cpit next to the original), because a syntax error stops Nginx from reloading.
Why Web Fonts Break on CDNs (The Same-Origin Trap)
Browsers treat font files loaded via CSS @font-face much more strictly than standard images or scripts. Under standard CORS rules, web fonts (.woff, .woff2, .ttf, .eot, .otf) are locked down by same-origin restrictions by default.
When your site loads from https://example.com and requests a font from https://cdn.example.com/fonts/inter.woff2, the browser treats it as a cross-origin request. If the CDN’s response doesn’t explicitly return an Access-Control-Allow-Origin header, the browser refuses to use the font.
The issue almost always traces back to your origin Nginx server. If Nginx serves the font without CORS headers, your CDN faithfully caches that headerless response and serves it to users worldwide.
Step 1: Confirm the Missing Header with cURL
Before touching your Nginx configs, check what your origin is actually sending back when a cross-origin request arrives. You can reproduce the browser’s request with curl.
Run this from your own computer’s terminal, with your font URL and your site’s address as the Origin:
curl -I -H "Origin: https://example.com" https://cdn.example.com/fonts/inter.woff2If CORS is broken, the response looks something like this example:
HTTP/2 200
server: nginx/1.24.0
date: Mon, 24 Feb 2025 14:22:10 GMT
content-type: font/woff2
content-length: 45210
last-modified: Wed, 15 Jan 2025 09:12:00 GMT
etag: "67877e80-b09a"
cache-control: public, max-age=31536000
accept-ranges: bytesNotice what’s missing: there is no Access-Control-Allow-Origin anywhere in that response. Without it, the browser blocks the font.
Step 2: Add Access-Control-Allow-Origin to the Font Location
To fix this for all standard font formats, match the file extensions inside a dedicated location block within your site configuration (usually at /etc/nginx/sites-available/example.com or /etc/nginx/conf.d/default.conf).
On the server, open your site config in an editor:
sudo nano /etc/nginx/sites-available/example.comAdd this regex block inside your server { ... } definition:
location ~* \.(?:woff|woff2|ttf|eot|otf)$ {
add_header Access-Control-Allow-Origin "*" always;
add_header Access-Control-Allow-Methods "GET, OPTIONS" always;
add_header Access-Control-Allow-Headers "*" always;
add_header Cache-Control "public, max-age=31536000, immutable";
access_log off;
log_not_found off;
try_files $uri =404;
}The always flag matters here. Without always, Nginx only adds the header to a fixed list of response codes (200, 201, 204, 206, 301, 302, 303, 304, 307 and 308). If an asset returns 404 or 403, the header is missing and the browser reports a CORS error that hides the real problem. See the Nginx ngx_http_headers_module documentation for more on how these flags behave.
Optional: Allow Only Your Own Domains with map
A wildcard Access-Control-Allow-Origin: * is fine for public open-source fonts. But if you’re running a multi-tenant app or need to prevent third parties from hotlinking paid, licensed fonts, a wildcard is either too permissive or won’t work with credentials.
You can’t supply a comma-separated list of domains in Access-Control-Allow-Origin according to the MDN Web Docs CORS guide. Instead, check the incoming $http_origin header and send it back only if it is on your allow list.
Drop this map directive inside the http { ... } block of /etc/nginx/nginx.conf:
map $http_origin $cors_origin {
default "";
"~^https?://(www\.)?example\.com$" "$http_origin";
"~^https?://(www\.)?staging\.example\.com$" "$http_origin";
"~^https?://app\.example\.com$" "$http_origin";
}Then reference that dynamic variable inside your font location block. No if is needed: Nginx skips an add_header whose value is empty.
location ~* \.(?:woff|woff2|ttf|eot|otf)$ {
add_header Access-Control-Allow-Origin $cors_origin always;
add_header Vary "Origin" always;
add_header Cache-Control "public, max-age=31536000, immutable";
try_files $uri =404;
}Adding add_header Vary "Origin" always; tells both browsers and CDN caches that the response changes based on who is asking. That only works if your CDN honors it: Cloudflare, for example, ignores Vary values other than Accept-Encoding by default. On a CDN like that, stay with the wildcard.
Step 3: Check for the add_header Inheritance Trap
This is the most common reason the fix seems not to work. If a child block (like a nested location or if) defines even a single add_header directive, it silently wipes out all add_header directives set in parent blocks.
Here is an example of what breaks:
server {
server_name cdn-origin.example.com;
# This parent header will be completely ignored below!
add_header Access-Control-Allow-Origin "*" always;
location ~* \.(?:woff|woff2)$ {
# Because this add_header exists, the parent one is dropped
add_header Cache-Control "public, max-age=31536000";
}
}To avoid this, either keep all your headers inside the specific location block, or move them into a snippet file that each block includes. Create the snippet on the server:
sudo nano /etc/nginx/snippets/cors-fonts.confDefine your shared security and caching headers in that file:
add_header Access-Control-Allow-Origin "*" always;
add_header Access-Control-Allow-Methods "GET, OPTIONS" always;
add_header Access-Control-Allow-Headers "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range" always;
add_header Access-Control-Expose-Headers "Content-Length,Content-Range" always;
add_header Cache-Control "public, max-age=31536000, immutable";Then pull the snippet into each font location block with include snippets/cors-fonts.conf;. The same rule affects cache headers; the guide to immutable Cache-Control and cache busting in Nginx shows it from the caching side.
Step 4 (If Needed): Answer OPTIONS Preflight Requests
Standard CSS @font-face GET requests don’t trigger preflight requests. A preflight only happens when a script fetches the font with non-standard request headers (a custom fetch() call that sets its own headers, for example); the browser then sends an HTTP OPTIONS request before grabbing the binary.
Nginx’s static file handler answers OPTIONS with 405 Not Allowed, so the preflight fails and the font download never starts. If you see that, return a clean 204 No Content from Nginx instead:
location ~* \.(?:woff|woff2|ttf|eot|otf)$ {
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin "*" always;
add_header Access-Control-Allow-Methods "GET, OPTIONS" always;
add_header Access-Control-Allow-Headers "*" always;
add_header Access-Control-Max-Age 86400;
return 204;
}
add_header Access-Control-Allow-Origin "*" always;
add_header Access-Control-Allow-Methods "GET, OPTIONS" always;
add_header Access-Control-Allow-Headers "*" always;
add_header Cache-Control "public, max-age=31536000, immutable";
try_files $uri =404;
}Once you’ve made the changes, test your config syntax and reload Nginx:
sudo nginx -t && sudo systemctl reload nginxnginx -t should report that the syntax is ok and the test is successful before the reload runs.
Step 5: Purge the CDN Edge Cache and Verify
Reloading Nginx fixes your origin, but your CDN edge servers are likely still caching the old, headerless font files. You have to invalidate those cached assets so the edge pulls a fresh copy from origin.
- Head to your CDN dashboard and purge your font paths (e.g.,
/fonts/*or specific URLs). - If your origin firewall only admits your CDN’s IP ranges (see how to restrict origin traffic to CDN IPs), run the check below against the CDN hostname, not the origin IP.
- To keep large purges from hammering your origin, make sure you’re protected against CDN cache stampedes with request collapsing.
From your own computer, test the CDN endpoint with cURL to verify the fix:
curl -I -H "Origin: https://example.com" https://cdn.example.com/fonts/inter.woff2Look for the access-control-allow-origin line in the response headers, as in this example:
HTTP/2 200
server: nginx/1.24.0
date: Mon, 24 Feb 2025 14:48:02 GMT
content-type: font/woff2
content-length: 45210
last-modified: Wed, 15 Jan 2025 09:12:00 GMT
etag: "67877e80-b09a"
access-control-allow-origin: *
access-control-allow-methods: GET, OPTIONS
cache-control: public, max-age=31536000, immutableThen reload your site with the browser’s cache disabled in DevTools. The CORS error should be gone and the custom font should render.
If It Still Doesn’t Work
- The header shows at the origin but not at the CDN: the CDN is still serving a cached copy, or a CDN rule strips or overrides response headers. Purge again and check the CDN’s header and CORS settings.
- The console says the header “contains multiple values”: both Nginx and your app (or the CDN) add
Access-Control-Allow-Origin. Browsers accept only one value, so remove one of them. - Only some pages fail when you use the map: the page’s origin is not on the list, often because
wwwand the bare domain differ. Add the missing origin to themapand reload. - curl returns 404 or 403 for the font: the request is not reaching the file. Check the
rootpath and the location regex before you look at CORS again.
Frequently Asked Questions
Why do images load fine from my CDN while fonts fail with CORS errors?
Browsers load a plain <img> without CORS checks; the image is displayed even when the response carries no CORS headers. Fonts are different: the CSS Fonts specification requires @font-face downloads to be made as CORS requests, largely so that licensed fonts cannot be hot-linked from other sites.
Can I just put Access-Control-Allow-Origin inside the http block?
You can, but it breaks easily. The moment any child server or location block uses add_header, that block drops all headers defined in the http block. Defining them directly in the font location block or using an include snippet is far more reliable.
Should I use a wildcard * or my specific domain for fonts?
Use the wildcard for open-source fonts like Inter or Roboto that anyone may use; it is standard and keeps CDN caching simple. If you’re hosting proprietary licensed fonts, use an Nginx map to restrict origins and pair it with Vary: Origin.
Why does my CDN still report CORS errors after restarting Nginx?
CDNs cache the entire HTTP response payload, including the headers. If the CDN grabbed the font before you fixed Nginx, it will keep serving that headerless response until you explicitly purge the cache or the file’s TTL expires.
Next Steps
With the header in place and the CDN purged, fonts load from the CDN on every page. Keep the curl check from Step 5 as a quick test after future Nginx changes, and make sure any new location block that serves fonts includes the same headers or the shared snippet.

