CORS and not 200 statusCode

My CORS works well with Nginx. APIs are designed to send a status code not 200 - example 401, 404, etc. - for bad input. The problem is that Chrome cancels / aborts the request if it receives a status code of not 200. For this reason, I cannot show the exact error on the web client.

How to avoid CORS non 200 status code errors?

+6
source share
2 answers

By default, Nginx adds headers for requests that it considers successful. You can add a header without regard to the response code by adding the always parameter to your add_header directive, for example.

 add_header 'Access-Control-Allow-Origin' '*' always; 
+7
source

You need to use the more_set_headers module.

with -s you can specify the status code

more_set_headers -s '404,400,403' 'Access-Control-Allow-Origin: http://domain.com';

However, if you did not install this module in nginx, you need to recompile it. compile it:

 wget http://nginx.org/download/nginx-1.7.8.tar.gz git clone https://github.com/openresty/headers-more-nginx-module.git tar -xzvf nginx-1.7.8.tar.gz cd nginx-1.7.8 ./configure --prefix=/opt/nginx --add-module=/path/to/headers-more-nginx-module make make install 
0
source

Source: https://habr.com/ru/post/970697/


All Articles