Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Fonts on another domain are blocked by CORS (nginx and Apache)

ErrorFont from origin has been blocked from loading by Cross-Origin Resource Sharing policy

Short answer

Fonts loaded with @font-face from another origin are fetched with CORS, so the font server must send Access-Control-Allow-Origin. In nginx use add_header with always, and watch inheritance: one add_header in a location drops the ones above. In Apache use Header set. Send Vary: Origin when you echo origins.

  • Applies to nginx 1.26, Apache 2.4, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask the font server for the file with an Origin header, as the browser does, and read the headers back.

    curl -s -D - -o /dev/null \
      -H "Origin: https://www.example.com" \
      https://static.example.net/fonts/site.woff2
  2. 2Match what you see, then jump to its fix

    1. 1The curl output has no Access-Control-Allow-Origin lineThe nginx font location sends no allow-origin headerFix
    2. 2The header is set at server level, yet font responses lack it, and the font location also sets Cache-Control or Expires headers with add_headerAnother add_header in the same block hides the CORS headerFix
    3. 3Apache serves the file but the curl output shows no allow-origin headerApache has no Header rule for the font, or only for success codesFix
    4. 4The font works for one site but fails for another, or fails only after a cache layer was addedA cache serves a font response made for another originFix

What does the error look like?

  • Font from origin has been blocked from loading by Cross-Origin Resource Sharing policy
  • Reason: CORS header 'Access-Control-Allow-Origin' missing

Cause 1 of 4

The nginx font location sends no allow-origin header

1Check

Search the nginx config for the header. nginx adds it only if you tell it to.

grep -R -n "Access-Control" /etc/nginx/

2Fix

Add the header in the location or server that serves the fonts. Use always so the header is also sent on error and redirect codes that add_header skips by default.

location /fonts/ {
    add_header Access-Control-Allow-Origin "https://www.example.com" always;
}

3Verify

Test the config, reload and repeat the first-step request.

sudo nginx -t && sudo systemctl reload nginx

Confirmed by nginx.org

Did this fix it?

Cause 2 of 4

Another add_header in the same block hides the CORS header

1Check

add_header directives are inherited from the level above only if the current level defines none. A single add_header in the location drops every inherited one.

sudo nginx -T | grep -n -E "location|add_header"

2Fix

Repeat the CORS add_header inside the location that holds the other add_header lines, or on nginx 1.29.3 or later set add_header_inherit merge in that location.

location /fonts/ {
    add_header Cache-Control "public, max-age=31536000" always;
    add_header Access-Control-Allow-Origin "https://www.example.com" always;
}

3Verify

Reload and request the font again.

curl -s -D - -o /dev/null -H "Origin: https://www.example.com" https://static.example.net/fonts/site.woff2 | grep -i -E 'access-control|cache-control'

Confirmed by nginx.org

Did this fix it?

Cause 3 of 4

Apache has no Header rule for the font, or only for success codes

1Check

Check that mod_headers is loaded and look for existing Header lines.

grep -R -n "Access-Control" /etc/apache2/

2Fix

Make sure mod_headers is enabled and set the header. Header always puts it in the table that is also used for error responses; plain Header uses the onsuccess table. Use set rather than add so the header is not duplicated.

# in the site config or .htaccess
Header always set Access-Control-Allow-Origin "https://www.example.com"

3Verify

Reload Apache, then repeat the first-step request.

curl -s -D - -o /dev/null -H "Origin: https://www.example.com" https://static.example.net/fonts/site.woff2 | grep -i access-control

Confirmed by httpd.apache.org

Did this fix it?

Cause 4 of 4

A cache serves a font response made for another origin

1Check

When the allowed origin changes per request, the response differs by the Origin request header, and caches need Vary: Origin to know it.

curl -s -D - -o /dev/null -H "Origin: https://other.example.org" https://static.example.net/fonts/site.woff2 | grep -i -E 'access-control|vary'

2Fix

Send Vary: Origin along with the allow-origin header whenever the value is picked from the request, or use a single fixed origin.

# nginx
add_header Vary Origin always;
# Apache
Header set Vary Origin

3Verify

Request the font with two different Origin values and confirm each response matches its own origin.

curl -s -D - -o /dev/null -H "Origin: https://www.example.com" https://static.example.net/fonts/site.woff2 | grep -i -E 'access-control|vary'

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Host fonts on the same origin as the site when you can, so no CORS is needed.
  • Keep the allowed origins in one config include shared by every font location.
  • Check the font URL with curl and an Origin header after each server or CDN change.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

The browser blocks your API response: no Access-Control-Allow-Origin header

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/orders. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

The browser received the response but refused to hand it to your script, because it did not carry an Access-Control-Allow-Origin header naming the page's origin.

4 causes, each with a fix

Web apps

A POST to a Google Apps Script web app returns 405 or a CORS error

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Apps Script web apps run doPost for POST and doGet for GET, and the Apps Script guides describe no other entry points.

3 causes, each with a fix

Web apps

OpenAPI does not work in an ASP.NET Core minimal API

404 Not Found at /openapi/v1.json

ASP.NET Core 9 and later generate OpenAPI documents with the Microsoft.AspNetCore.OpenApi package: AddOpenApi registers it and MapOpenApi serves /openapi/v1.json.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.