我的控制台看起来像这样
这是我的 nginx cors 设置
add_header 'Access-Control-Allow-Origin' 'http://beloveddais.com';
add_header 'Access-Control-Allow_Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' 'http://beloveddais.com';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Allow_Credentials' 'true';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
proxy_redirect off;
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
我的 Node 服务器看起来像这样
var allowCrossDomain = function(req, res, next) {
if('GET' == req.method){
res.header('Access-Control-Allow-Origin', 'http://beloveddais.com');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
}
if('POST' == req.method){
res.header('Access-Control-Allow-Origin', 'http://beloveddais.com');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
}
if('OPTIONS' == req.method){
res.header('Access-Control-Allow-Origin', 'http://beloveddias.com');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.sendStatus(200);
}
next();
在我的浏览器控制台上,我收到了多个 header ,但只有一个被接受。我该如何解决?我真的需要有人帮助我。
最佳答案
Access-Control-Allow-Origin
header 应仅包含一个值作为允许的来源,或者您可以使用 * 来允许所有来源。
正如 @TarunLalwani 所评论的,您将 http://beloveddais.com
作为值添加到 NGINX 配置中和 OPTIONS
请求的代码中的 Access-Control-Allow-Origin
header 中:
add_header 'Access-Control-Allow-Origin' 'http://beloveddais.com';
并且
res.header('Access-Control-Allow-Origin', 'http://beloveddias.com');
根据您选择的设计删除其中一个,应该没问题。
关于node.js - 需要帮助修复我的 nginx 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48782509/