我在 Nginx 上运行 CORS 时遇到了一个奇怪的问题,CORS 在所有情况下都能正常工作,但只有一种情况是服务器响应 403 http 响应时除外。
基本上,当我使用正确的凭据登录时,cors 请求工作正常,但是当我提供错误的登录凭据时,服务器(后端)会响应 403 状态,并且出现以下错误
"NetworkError: 403 Forbidden - <a href="http://mydomain.com/v1/login" rel="noreferrer noopener nofollow">http://mydomain.com/v1/login</a>"
login
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at <a href="http://mydomain.com/v1/login" rel="noreferrer noopener nofollow">http://mydomain.com/v1/login</a>. This can be fixed by moving the resource to the same domain or enabling CORS.
如果凭据正确,我就不会收到此错误,并且一切正常。
我已经完成了启用 CORS 的配置,它似乎对其他一切都运行良好。
以下是请求 header
请求 header
User-Agent:Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0
Referer:<a href="http://abc.mydomain.com/" rel="noreferrer noopener nofollow">http://abc.mydomain.com/</a>
Pragma: no-cache
Origin: <a href="http://abc.mydomain.com" rel="noreferrer noopener nofollow">http://abc.mydomain.com</a>
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, <em>/</em>
响应 header
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
最佳答案
nginx(>=1.75) 的一个选项是在 add_header 中指定 always
参数:
If the always parameter is specified (1.7.5), the header field will be added regardless of the response code.
关于nginx - Cors 在 Nginx 上阻止状态为 403 的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24119705/