Tạo CORS Header
Tạo cấu hình CORS cho các máy chủ web và framework phổ biến
Origin ký tự đại diện (*) không thể dùng chung với Access-Control-Allow-Credentials: true.
Access-Control-Allow-Methods
Các header phản hồi cách nhau bằng dấu phẩy được phép đọc từ JavaScript phía client.
Thời gian lưu cache preflight tính bằng giây (mặc định 86400).
Các Header phản hồi HTTP
Đoạn cấu hình 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;
}