在nginx.conf 配置文件中设置如下!
location /
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,userId,token';
add_header 'Access-Control-Max-Age' 3600;
#add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'XDomainRequestAllowed' '1';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,userId,token';
add_header 'Access-Control-Max-Age' 0;
#add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'XDomainRequestAllowed' '1';
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,userId,token';
add_header 'Access-Control-Max-Age' 0;
#add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'XDomainRequestAllowed' '1';
}
以上是根据具体的 POST、PUT、GET等方式的配置:
还可以直接在 server节点下直接配置:
add_header 'Access-Control-Allow-Origin' '*'; 就是最简单的配置