第一种,允许该站点所有请求可跨域请求
#允许跨域请求
#放在诸如 include enable-php-.conf;这种的前面
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
第二种,允许该站点某个目录或某种文件可跨域请求
#放在诸如 include enable-php-.conf;这种的前面
#使用正则规定哪些目录或文件在请求时会添加可跨域header
location ~ /(.*)\.svg
{
#允许跨域请求
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
}
例如允许某个目录或者文件夹下的js,json跨域请求
location ~ .*\.(js|json)?$
{
#允许跨域请求
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
}