CORS 標頭產生器
為常見網頁伺服器與框架產生 CORS 設定。
萬用字元來源 (*) 不得與 Access-Control-Allow-Credentials: true 同時使用。
Access-Control-Allow-Methods
以逗號分隔開放給用戶端 JavaScript 讀取的回應標頭。
預檢快取時間,以秒為單位(預設為 86400)。
HTTP 回應標頭
Nginx Location 設定碼片段
location /api/ {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' 'https://example.com';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization';
add_header 'Access-Control-Max-Age' 86400;
add_header 'Content-Type' 'text/plain; charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
add_header 'Access-Control-Allow-Origin' 'https://example.com' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
}