我曾经用这个配置运行 nginx v1.6:
location / {
alias /some/path/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header uuid $uuid;
more_set_headers 'Access-Control-Allow-Origin: $http_origin';
more_set_headers 'Access-Control-Allow-Credentials: true';
more_set_headers 'Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS';
more_set_headers 'Access-Control-Allow-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,access_token,__setXHR_';
if ($request_method = 'OPTIONS') {
more_set_headers 'Access-Control-Allow-Origin: $http_origin';
more_set_headers 'Access-Control-Allow-Credentials: true';
more_set_headers 'Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS';
more_set_headers 'Access-Control-Allow-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,access_token,__setXHR_';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
}
Run Code Online (Sandbox Code Playgroud)
自从我升级到 nginx v1.10.x 后,“more_set_headers”不再起作用,我总是通过 add_header 'blablabla' 更改它;
现在看起来像这样:
location / {
alias /some/path/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header uuid $uuid;
add_header 'Access-Control-Allow-Origin: $http_origin' always;
add_header 'Access-Control-Allow-Credentials: true' always;
add_header 'Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,access_token,__setXHR_' always;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin: $http_origin' always;
add_header 'Access-Control-Allow-Credentials: true' always;
add_header 'Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,access_token,__setXHR_' always;
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我现在访问网站时,出现以下错误:
无法加载https://mywebsite/auth/login:对预检请求的响应未通过访问控制检查:“Access-Control-Allow-Origin”标头包含多个值“$http_origin: always”,但只有一个是允许。因此,不允许访问Origin ' https://mywebsite '。
我应该改变什么才能使它工作?我有点卡在那里。
我不记得我今天早些时候在哪里读到的。我丢失了页面。但是,就我而言,答案是:
您不能在堆栈中设置多个 cors 标头。鸡蛋 nginx 和 django。只有其中一个可以完成这项工作,否则标题中会有多个值。
通过从我的 api 中删除 cors 处理并只让 nginx 这样做,错误消失了。
我会更新它并在找到时参考来源。由于在多个论坛和平台上很难在 Google 上找到此信息,因此我决定在 SO 上回答这个明显的主题。
2021 年更新 - 更多解释
对于好奇的人,再详细说明一下。CORS 标头实际上只能包含单个值。如果它*允许所有,那没问题,但是如果你想允许两个特定的 IP 怎么办?您可能希望在标头中写入这两个 IP,但这是无效的。
当使用 Web 框架 ala Express.js 或 Django 时,所有这些包在幕后所做的如下。
它们将传入请求引用标头中的 IP 与开发人员提供的 IP 列表进行匹配。如果匹配,响应将在 CORS 标头中添加该特定 IP。没有其他IP。
如果您想实现自己的 CORS 中间件,则需要复制所描述的行为。
Nginx 很可能会做同样的事情,而双方都不会检查标头中是否已经存在值。他们只是根据匹配添加值。所以结果将是一个无效的 CORS 标头。
| 归档时间: |
|
| 查看次数: |
14966 次 |
| 最近记录: |