
我們收到此錯誤:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://stats.g.doubleclick.net
在我們的 nginx 設定中,我們有:
add_header X-Frame-Options SAMEORIGIN always;
add_header X-Xss-Protection "1; mode=block" always;
add_header X-Content-Type-Options nosniff always;
add_header Content-Security-Policy "default-src 'self' https: data: 'unsafe-inline' 'unsafe-eval';" always;
add_header Referrer-Policy "no-referrer-when-downgrade";
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains";
不確定該指令及其語法如何僅允許doubleclick.net
我們自己的域。
答案1
在 a 內部location
(proxy_pass
例如使用指令),您可以動態產生正確的標頭:
if ($http_origin ~ '^(?:https?://)?(?:[a-z0-9_.-]+\.)?(?:doubleclick\.net|example\.com)$')
{
more_set_headers 'Access-Control-Allow-Origin: $http_origin';
more_set_headers 'Access-Control-Allow-Methods: GET, OPTIONS, POST';
}
如果您不明白正規表示式的內容,請詢問。
例如,如果您沒有 Ubuntu,則可以替換more_set_headers
為。請注意語法略有不同。add_header
nginx-extras
該解決方案對於該目的來說足夠安全。據我所知,在標準瀏覽器中偽造原始標頭並不容易。你curl
可以(測試一下):
curl -H 'Origin: https://example.com' -I -L 'https://example.com' 2>/dev/null | grep -i access-control
和:
curl -H 'Origin: https://stats.g.doubleclick.net' -I -L 'https://example.com' 2>/dev/null | grep -i access-control
兩者都應該返回兩個access-control
標頭。未經測試。