我是一个非常新的用户,如果我违反了任何规则,请提前道歉。这里是我面临的问题,并需要建议。
我有一个Chrome扩展程序,它与Gmail一起工作,并通过Rails应用程序的Phusion Passenger服务器从我的Web服务器上的API获取信息。
我的Nginx版本是nginx version: nginx/1.15.8,Phusion Passenger版本为Phusion Passenger Enterprise 6.0.1。
我的Nginx服务器CORS设置如下:
####### CORS Management ##########
add_header 'Access-Control-Allow-Origin' 'https://mail.google.com,https://*.gmail.com';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE, HEAD';
add_header Referrer-Policy "no-referrer";
add_header Pragma "no-cache";
##################################
此前在 Chrome 84 版本中可以正常工作,但在最新的 Chrome 85 版本中,开始出现以下 CORS 错误:
########## 在 Chrome 85 中开始出现的错误 ############
因跨域资源共享(CORS)策略而被阻止访问 'https://my-site.com/',其来源为 'https://mail.google.com'
请求的资源上没有 'Access-Control-Allow-Origin' 头,已被阻止访问。
##########################################
之后,我根据各种来源和博客的建议/参考更新了 CORS 设置,现在更新后的 CORS 设置如下:
location / {
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, OPTIONS' always;
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type' always;
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8' always;
add_header 'Content-Length' 0 always;
return 204;
}
if ($request_method = 'POST') {
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, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type' always;
}
if ($request_method = 'GET') {
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, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type' always;
}
}
############################################
在Nginx更新此设置后,CORS错误已经消失,但是当扩展程序进行API调用时,现在会从服务器收到401未授权错误。
我尝试了所有的方法,但无法解决它。是否有什么我错过或者做的不同的地方?
请帮帮我!