CORS Header Generator — Access-Control-Allow-Origin for Nginx, Express and Caddy
Generate Nginx, Express & Caddy CORS headers for allowed origins & methods.
Paste the map block at http { } level and the add_header/if lines inside the API location. add_header is repeated inside if because Nginx does not inherit it into that block.
When to use the CORS (Access-Control-Allow-*) Header Generator
Several origins, safely
Access-Control-Allow-Origin can hold only one origin. For several origins this config matches the request's Origin against your list and echoes it, with Vary: Origin so caches do not mix them up.
Preflight answered in full
OPTIONS requests get a 204 with the allowed methods, headers and Max-Age, so PUT, DELETE or an Authorization header no longer fail in the browser.
Browser rules checked
Combinations browsers reject are flagged: * together with credentials, origins with a path or trailing slash, and a Max-Age above Chrome's 7,200-second cap.
Tested with cross-origin fetch
Every config runs on a real server, then Chrome calls the API from allowed and forbidden origins, preflight with cookies included.
Frequently asked questions
Why do I still get CORS errors with the headers set?
Often because the headers are only sent on successful responses. In Nginx, add_header without always is not sent on 4xx/5xx errors, so this tool always adds always.
Do GET and POST need to be listed?
GET, HEAD and POST are allowed without being listed, as long as they use standard headers. The method list matters for PUT, PATCH, DELETE and others.
Can I allow every subdomain?
Yes, write https://*.example.com. The config uses a regular expression that matches any subdomain but still refuses look-alikes such as example.com.evil.test.
What is a preflight request?
Before sending PUT, PATCH or a request with an Authorization header to another site, the browser first sends an OPTIONS request asking whether that method and those headers are allowed. The answer is cached for Max-Age so it is not repeated on every request.
Similar tools
- Google Analytics UTM Campaign Link BuilderBuild GA4 campaign links: encoded utm_ values, existing parameters and the #fragment kept, lowercase and space options, presets, many URLs at once and .txt/.csv export.
- Apache .htaccess 301/302 Redirect GeneratorGenerate Apache mod_rewrite 301 permanent redirects and HTTPS enforcement.
- Nginx return 301 & rewrite Rule GeneratorGenerate fast Nginx server block 301 redirects and non-www canonicalization.
- Cloudflare Bulk & Dynamic Redirect Rule BuilderExport CSV lists for Cloudflare Bulk Redirects or dynamic rule expressions.