【发布时间】:2020-02-26 07:06:01
【问题描述】:
目前,我的 apache2.4 虚拟主机中有这个配置:
Header always append Access-Control-Allow-Origin "https://www.testdomain.local"
Header always append Access-Control-Allow-Origin "https://testdomain.local"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
RewriteEngine on
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
但是我不断收到此错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www.testdomain.local/ads?_=1572477483136. (Reason: CORS header ‘Access-Control-Allow-Origin’ does not match ‘https://www.testdomain.local, https://testdomain.local’).
我知道我可以通过将 Access-Control-Allow-Origin 设置为通配符来解决此问题,但这并不安全。
【问题讨论】:
-
您的 apache 配置在前两行设置了两次 Access-Control-Allow-Origin 标头。你不能那样做。如果你设置了两次,浏览器会合并这些值,所以浏览器最终会认为它有两个值。但是浏览器只允许它有一个值。这就是浏览器在该错误消息中告诉您的内容。
-
好吧,我同意。根据文档将其更改为“附加”仍然不能解决此问题:httpd.apache.org/docs/current/mod/mod_headers.html
标签: apache cors http-headers webserver cross-domain