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

标签 nginx cors http-status-code-403

我在 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/

相关文章:

api - Laravel 5.2 在中止时将 Http 响应状态代码设置为 403

ubuntu - 使用 Let's encrypt "Welcome to Nginx"而不是 Rails 应用程序的 Nginx

php - Codeigniter 的 nginx 重写规则无法正常工作

node.js - CORS 在 AWS、NodeJs 和其他 Web 服务中如何工作?

jquery - IE11 的 CORS 请求

php - 提交简单的 PHP 表单时出现禁止错误

ElasticSearch:主机响应单个网站的 403 错误

openid - 使用 openid 作为静态内容身份验证的最简单方法

node.js - 如何使负载平衡对同一 "group"中的多个客户端具有粘性?

javascript - Html <img src=...> 有效但 JS 图像加载导致 CORS 错误