CORS Header Generator: Configure Cross-Origin Sharing in Seconds
Learn how to generate secure, standard-compliant CORS headers for Nginx, Apache, and Express with the free online CORS Header Generator.
Table of Contents
Cross-Origin Resource Sharing, or CORS, is the browser mechanism that decides whether a web page running on one origin is allowed to fetch resources from another. Get it right and your API works seamlessly from any frontend. Get it wrong and you are staring at the dreaded red console error: No 'Access-Control-Allow-Origin' header is present on the requested resource. Configuring CORS by hand is error-prone because every server (Nginx, Apache, Express) has its own syntax, and small mistakes can quietly open your API to the entire internet.
The CORS Header Generator removes that friction. You describe the policy you want β allowed origins, methods, headers, credentials, max-age β and the tool emits ready-to-paste configuration for Nginx, Apache, and Express.js, complete with security warnings when a combination is dangerous.
In this guide we will walk through why CORS matters, how the generator works under the hood, the headers it produces, and concrete recipes you can drop straight into production. Whether you are hardening a public API or wiring up cookie-based authentication, you will leave with a copy-paste-ready config.
Why Use the CORS Header Generator?
- Instant multi-server output β Instead of hunting through three different docs, you enter your policy once and get valid snippets for Nginx, Apache, and Express simultaneously.
- Security-first validation β The tool flags dangerous combinations, such as using the wildcard * origin together with Access-Control-Allow-Credentials: true, which browsers reject outright.
- Preflight handling built in β Every output includes the correct OPTIONS handling so preflight requests succeed without an extra round of debugging.
- Standard compliant β Generated headers follow the Fetch specification, so behaviour is consistent across Chrome, Firefox, Safari, and Edge.
- Copy-to-clipboard convenience β Each config block has its own copy button, so you can paste straight into your nginx.conf or server.js without manual cleanup.
- No lock-in, no signup β The generator runs entirely in your browser, produces plain text, and never asks for an account.
Key Features
| Feature | What It Does |
|---|---|
| Origin validation | Checks each allowed origin for correct scheme, host, and port format. |
| Method selector | Toggles GET, POST, PUT, DELETE, PATCH, OPTIONS, and HEAD individually. |
| Header allowlist | Picks from Content-Type, Authorization, X-Requested-With, Accept, and Origin. |
| Credentials toggle | Enables Access-Control-Allow-Credentials with automatic wildcard warnings. |
| Max-age control | Sets how long browsers cache preflight results to reduce OPTIONS traffic. |
| Exposed headers | Declares which response headers JavaScript is allowed to read. |
- Live security warnings appear the moment a configuration becomes unsafe, so you catch mistakes before they ship.
- Exposed-headers support lets you surface custom response headers like X-Request-Id to your frontend without leaking everything.
- Per-format copy buttons mean you grab exactly the Nginx block or the Apache block or the Express middleware β never a mixed bundle.
How to Use the CORS Header Generator
- Set your allowed origins. Enter one origin per line (for example https://app.example.com), or use * for a fully public, credential-free API.
- Pick the HTTP methods your endpoint should accept. Leave out anything you do not use β every permitted method widens the attack surface.
- Choose request headers the browser is allowed to send. Common picks are Content-Type and Authorization, but you can add custom ones.
- Toggle credentials if you rely on cookies or HTTP auth, and set a sensible max-age (86400 seconds is a good default for caching preflight).
- Copy the output for your stack β Nginx add_header directives, Apache Header set lines, or an Express cors() options object β and paste it into your server config.
Understanding CORS Headers
CORS is enforced by the browser, not the server. When a script on https://app.example.com calls https://api.example.com/data, the browser checks the response for specific headers that explicitly grant the calling origin permission. There are six headers that matter:
- Access-Control-Allow-Origin β The single most important header. It echoes back the calling origin (https://app.example.com) or uses * to allow any origin (only when credentials are off).
- Access-Control-Allow-Methods β Lists the HTTP methods the origin is permitted to use, for example GET, POST, PUT, DELETE.
- Access-Control-Allow-Headers β Whitelists request headers the browser may attach, such as Content-Type and Authorization.
- Access-Control-Allow-Credentials β Set to true when cookies, Authorization headers, or TLS client certificates should accompany cross-origin requests.
- Access-Control-Max-Age β Tells the browser how long (in seconds) it may cache the result of a preflight OPTIONS request, cutting down redundant network round-trips.
- Access-Control-Expose-Headers β Lets frontend JavaScript read otherwise-forbidden response headers by listing them explicitly.
Preflight requests deserve special attention. For any request that is not a "simple" GET or POST with a basic Content-Type, the browser first sends an OPTIONS request asking βam I allowed to do this?β The server answers with the allow-methods and allow-headers. Only if that answer satisfies the request does the real call go out. This is why every config produced by the generator includes proper OPTIONS handling β miss it and non-simple requests fail silently.
Credentials are where most teams get burned. Sending Access-Control-Allow-Credentials: true tells the browser to include cookies on cross-origin calls, but the spec forbids combining this with a wildcard origin. You must echo a specific origin instead, and the generator enforces this rule automatically.
Practical Use Cases
Securing a Public API
For a read-only public API with no authentication, the simplest safe policy is a wildcard origin and no credentials:
location /api/public/ {
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, HEAD, OPTIONS' always;
add_header 'Access-Control-Max-Age' 1728000 always;
}
The generator produces exactly this shape when you select GET/HEAD/OPTIONS and leave credentials off, so any website can read your data without risking cookie leakage.
Cookie-Based Auth with Credentials
When your API lives behind session cookies, you need credentials enabled and a strict origin allowlist. In Express:
const cors = require('cors');
app.use(
cors({
origin: ['https://app.example.com', 'https://admin.example.com'],
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'],
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'],
exposedHeaders: ['X-Request-Id'],
maxAge: 86400,
})
);
Notice the explicit origin list instead of * β that is mandatory when credentials are involved, and the generator will warn you if you forget.
Third-Party Widget Embedding
If you ship a JavaScript widget that other sites embed, you typically need to allow many origins at runtime. The recommended pattern is to read the incoming Origin header and reflect it back when it is on your allowlist, rather than hard-coding a list in the response. The generator's Apache output can serve as the starting point:
<IfModule mod_headers.c>
SetEnvIf Origin "^(https://(app|embed|cdn)\.example\.com)$" CORS_ALLOW_ORIGIN=$1
Header set Access-Control-Allow-Origin "%{CORS_ALLOW_ORIGIN}e" env=CORS_ALLOW_ORIGIN
Header set Access-Control-Allow-Credentials "true" env=CORS_ALLOW_ORIGIN
Header set Access-Control-Allow-Methods "GET, POST, OPTIONS" env=CORS_ALLOW_ORIGIN
</IfModule>
This dynamic reflection keeps your allowlist tight while supporting arbitrary subdomains.
Development and Localhost
During local development your frontend often runs on http://localhost:3000 while the API sits on http://localhost:8080. Add the localhost origin explicitly in the generator, copy the Express snippet, and you avoid the localhost CORS dance that trips up so many onboarding sessions.
Best Practices
- Never combine * with credentials β browsers block it, and it is the most common CORS bug in production.
- Allowlist origins explicitly rather than reflecting the Origin header unconditionally, which can open a CSRF-style hole.
- Minimise permitted methods β if an endpoint only reads data, expose GET and HEAD and nothing else.
- Set a generous max-age (86400 seconds or more) so browsers cache preflight responses and cut OPTIONS traffic.
- Expose only the headers you need β every extra exposed header is information your frontend did not require.
- Test preflight paths by triggering a non-simple request (for example a Content-Type: application/json POST) and confirming the OPTIONS response is correct before deploying.
Generate Your CORS Headers Now
Stop wrestling with half-remembered Nginx syntax or guessing which headers your frontend needs. Open the CORS Header Generator, configure your policy in under a minute, and paste production-ready output directly into your server. It validates every dangerous combination, handles preflight automatically, and gives you Nginx, Apache, and Express snippets side by side β all in your browser, with no signup required.
Related Tools You Might Like
- HTTP Headers Viewer β Inspect the response headers any server actually sends, perfect for verifying your CORS config after deploy.
- Nginx Config Generator β Build complete Nginx server blocks with caching, gzip, and TLS alongside your CORS rules.
- JWT Decoder β Decode and inspect JSON Web Tokens to debug the Authorization headers your CORS policy now permits.
Happy configuring!