Cors在Nginx上阻止状态为403的请求

时间:2014-06-09 11:51:26

标签: nginx cors http-status-code-403

我在Nginx上运行CORS时遇到一个奇怪的问题,当服务器以403 http响应响应时,CORS在一切情况下工作正常。

基本上,当我使用正确的凭据登录时,cors请求工作正常,但是当我提供错误的登录凭据时,服务器(后端)响应403状态,我收到以下错误 "NetworkError: 403 Forbidden - http://mydomain.com/v1/login" login Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://mydomain.com/v1/login. This can be fixed by moving the resource to the same domain or enabling CORS. 如果凭据是正确的,我不会收到此错误,一切都运作正常。

我已经完成了启用CORS的配置,它似乎可以正常运行。

以下是请求标题

请求标题 User-Agent:Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0 Referer:http://abc.mydomain.com/ Pragma: no-cache Origin: http://abc.mydomain.com Host: www.mydomain.com Content-Type: application/json;charset=utf-8 Content-Length: 74 Connection: keep-alive Cache-Control: no-cache Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Accept: application/json, text/plain, / 响应标头 Server: nginx/1.4.1 Date: Tue, 10 Jun 2014 05:28:30 GMT Content-Type: application/json; charset=utf-8 Content-Length: 76 Connection: keep-alive

2 个答案:

答案 0 :(得分:3)

nginx(> = 1.75)的选项是在add_header中指定always参数:

  

如果指定了always参数(1.7.5),则标题字段将为   无论响应代码如何都添加。

答案 1 :(得分:1)

我假设你使用add_header指令在nginx配置中添加CORS头。

Nginx模块引用有关add_header

的状态
  

如果响应代码等于200,201,204,206,301,302,303,304或307,则将指定字段添加到响应头。

要解决问题,您可以使用ngx_headers_more模块将CORS标头设置为错误响应。

more_set_headers 'Access-Control-Allow-Origin: $http_origin';

more_set_headers 'Access-Control-Allow-Headers: Content-Type, Origin, Accept, Cookie';