How to Fix an Nginx Cors Error Fast
How can I fix a CORS error in Nginx?
You fix an Nginx CORS error by adding the Access-Control-Allow-Origin header to your server configuration block and explicitly handling HTTP OPTIONS preflight requests.

Browser security restricts scripts from reading cross-origin responses unless the server explicitly grants permission via HTTP headers (MDN).
When a web application makes a request to a different domain, port, or protocol, modern browsers enforce the Same-Origin Policy. If Nginx does not return the correct CORS headers, the browser blocks the response. You can resolve this issue by updating your Nginx site configuration file with specific header rules.
How to configure Nginx for basic CORS requests
To enable basic cross-origin resource sharing, you add specific header directives inside your server or location block. You must include headers for the allowed origin, methods, and headers.

Open your Nginx configuration file and add the following directives inside your location block:
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, X-Requested-With';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
add_header 'Access-Control-Allow-Origin' '' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, X-Requested-With' always;
}
After saving the configuration file, test the syntax by running sudo nginx -t in your terminal. If the test returns successful, reload the service using sudo systemctl reload nginx to apply the changes without dropping active connections.
Why do CORS errors still appear after configuration?
CORS errors often persist because the Nginx add_header directive drops headers on error responses unless you append the always parameter. By default, Nginx only applies these headers to successful HTTP status codes like 200, 201, or 204. When your backend application returns a 401 Unauthorized or 500 Internal Server Error status, the browser prevents frontend JavaScript from accessing the response if the CORS headers are missing from that error response (MDN).

Another common mistake involves nested location blocks. If you define an add_header directive inside a nested location block, Nginx completely ignores all inherited headers from upper server blocks. You must explicitly redefine all required CORS headers within every active location block that handles API requests.
How to confirm the Nginx CORS fix works
You confirm your configuration works by sending a test request using the curl command-line tool. Run the following command in your terminal:

curl -I -H "Origin: http://example.com" -H "Access-Control-Request-Method: GET" -X OPTIONS https://api.yourdomain.com/endpoint
When the configuration works correctly, the terminal output displays the Access-Control-Allow-Origin: * header alongside an HTTP/1.1 204 No Content status code. You can then test your React or frontend application to verify that an nginx cors error disappears.
Related guides
Frequently asked questions
What is an Nginx server?
Nginx is an open-source web server that also functions as a reverse proxy, load balancer, and HTTP cache. It handles incoming web traffic and routes requests to appropriate backend applications.
Should CORS be enabled with a wildcard?
Using a wildcard origin is generally discouraged in production environments because it allows any website to read your API data. You should replace the wildcard with your specific frontend domain when handling sensitive user data or authenticated requests.
How to fix 403 forbidden Nginx error?
A 403 Forbidden error typically occurs due to incorrect file permissions, a missing index file, or restricted IP access rules in your configuration. You can resolve it by verifying root directory permissions and checking the nginx cors error log located at /var/log/nginx/error.log.