CORS 头生成器
为常见 Web 服务器和框架生成 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;
}