All Tools View Categories About Contact Privacy

Nginx CORS Headers Config Generator

Open your API to browsers with correct CORS headers.

Runs entirely in your browser - nothing is uploaded and no cloud connection is made.
Your CORS config will appear here.
-
lines
-
blocks
-
locations

About Nginx CORS Headers Config Generator

Cross-origin resource sharing is what lets a browser based at one domain call an API at another, and nginx can emit the right headers without an application change. The hard parts are the OPTIONS preflight, the always flag that keeps headers on error responses, and the rule that you cannot combine a wildcard origin with credentials. The Nginx CORS Headers Config Generator builds a correct location that sets the allow headers, answers preflight with 204, and refuses the invalid wildcard-plus-credentials combo.

At the heart of the configuration are a handful of directives. add_header (Access-Control-Allow-Origin) declares which origins may read the response, using always so it survives error responses. add_header (Access-Control-Allow-Methods) lists the HTTP methods permitted for cross-origin calls. add_header (Access-Control-Allow-Headers) lists request headers the client may send, such as Content-Type or Authorization. add_header (Access-Control-Max-Age) caches the preflight result for the given number of seconds. add_header (Access-Control-Allow-Credentials) permits cookies and authorization to be sent when origins are explicit. if ($request_method = "OPTIONS") short-circuits preflight requests and returns 204 with the CORS headers. Together they shape how the server behaves, and the tool assembles them in the right context so the result is valid on the first try.

Common mistakes are easy to make. Omitting always means CORS headers vanish on 4xx/5xx responses, breaking error handling in the browser, so the tool adds always everywhere. Pairing a wildcard origin with credentials is forbidden by the spec, so the generator rejects it and asks for a real origin list. Forgetting the OPTIONS branch leaves preflight unanswered and the browser blocks the real request, so it is included by default. The generator anticipates each of these and either sets a safe default or rejects the input with a clear message before anything is written to your clipboard.

Validation is strict because small configuration errors fail in subtle ways. Every input is checked for plausibility, and after the block is assembled it is re-parsed by a built-in tokenizer so unbalanced braces, missing semicolons or stray characters cannot reach your clipboard. Stat cards report line and block counts, and copy, download and print exports are one click away. Everything runs in your browser; nothing you type is transmitted to any server.

In practice this block drops into any standard nginx install. Save the output as a file under /etc/nginx/conf.d/ (or sites-available with a symlink), run nginx -t to confirm the syntax, then reload with nginx -s reload. Because the generator emits a single, self-contained server block with no hidden dependencies, it composes cleanly with your existing caching, logging and security configuration without directive collisions.

Beyond producing correct config, the tool is a reference you can read back and learn from. Each control maps to a real nginx directive, the sample button shows a complete working block in seconds, and clearing the form resets every field to its safe default. Standardising on a generator like this removes per-developer variation, keeps your configuration readable, and gives you a repeatable, auditable setup that passes nginx -t on the first try.

When something looks wrong in production, the first move is always to re-run nginx -t and inspect /var/log/nginx/error.log; most failures surface there with a line number. The access log records every request, so a sudden spike or a wall of 499 responses points straight at backend or timeout problems the generator helps you avoid in the first place.

This server block is designed to sit alongside - not fight - your other configuration. Because it declares its own server_name and a single, self-contained set of directives, you can drop it into conf.d without worrying about collisions with global caching, logging or security snippets that live elsewhere in the nginx tree.

For a production site, pair this block with TLS termination: serve on 80 for the redirect or health checks, and place the encrypted listener (or a front-end load balancer / CDN) in front so clients always speak HTTPS. The generator keeps that boundary clean so the two layers compose instead of overlapping.

If a change ever needs to be undone, the output is plain text you control: delete the file from conf.d, re-run nginx -t, and reload. There is no database and no hidden state, so rolling back is as simple as restoring the previous version from version control or your own backup.

Performance and correctness both benefit from explicit configuration. Defaults baked into the generator reflect current best practice rather than decades-old forum snippets, so the block you ship today will not surprise you with deprecated directives or insecure fallbacks six months from now.

For teams, a generated block is also documentation. New engineers can read the exact directives in place, compare them against the sample, and learn the relevant nginx behaviour without reverse-engineering a hand-maintained file that drifted from its original intent.

Features

  • add_header (Access-Control-Allow-Origin) - declares which origins may read the response, using always so it survives error responses.
  • add_header (Access-Control-Allow-Methods) - lists the HTTP methods permitted for cross-origin calls.
  • add_header (Access-Control-Allow-Headers) - lists request headers the client may send, such as Content-Type or Authorization.
  • add_header (Access-Control-Max-Age) - caches the preflight result for the given number of seconds.
  • add_header (Access-Control-Allow-Credentials) - permits cookies and authorization to be sent when origins are explicit.
  • if ($request_method = "OPTIONS") - short-circuits preflight requests and returns 204 with the CORS headers.
  • Self-verifying output re-parsed before display.
  • Copy, Download and Print exports.
  • Load-sample button fills realistic values.
  • Statistics cards for quick checks.
  • Runs entirely in your browser - nothing uploaded.

How to Use

  1. Enter the domain and location path to expose.
  2. List allowed origins, one per line, or use * for any.
  3. Enter the allowed methods and headers.
  4. Set the preflight max age in seconds.
  5. Toggle credentials and preflight handling.
  6. Click Generate (or Load sample) and review the location.
  7. Drop it into your server block and reload.

Examples

Example 1 - Open API wildcard origin with preflight for a public read API.

Example 2 - Two origins explicit allowlist of two partner sites.

Example 3 - With credentials credentials on with explicit origins for a logged-in SPA.

Example 4 - No preflight preflight handling off when callers never send complex requests.

Example 5 - Wildcard rejected wildcard plus credentials is reported as an error before generation.

Benefits

  • Correct preflight 204 handling.
  • Always flag on every CORS header.
  • Wildcard plus credentials rejected.
  • Explicit origin allowlist support.
  • Self-checked output re-parsed before display.
  • Private: everything runs in your browser.

Frequently Asked Questions

What is a preflight request?
Browsers send an OPTIONS request before a non-simple request to check permissions; answering 204 with the right headers lets the real request proceed.
Why always on add_header?
The always parameter ensures the CORS headers are sent even on error responses, which many SPAs depend on.
Can I use * with credentials?
No - the spec forbids wildcard origin with credentials, so the generator rejects that combination and asks for explicit origins.
Why list multiple origins?
nginx does not natively echo a list, so joined origins are a simple static allowlist; for dynamic echo use a map.
Where do these go?
Inside the location that serves your API or assets.
Do I still need this with a CDN?
Often the CDN handles CORS; only add it if your origin must emit the headers itself.
Is the output validated?
Yes - the block is re-parsed by a built-in tokenizer before display.
Is anything uploaded?
No. Everything runs in your browser.