server
{}
里添加代码:
location / {
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET';
index index.php index.html index.htm;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, DELETE, PUT, 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, Access-Control-Expose-Headers, Token, Authorization';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
}
注意:
1.如果配置了伪静态,location /会冲突 ,则可以将以上代码复制进伪静态文件里面
2. tp5自带跨域配置,在config.php里
'cors_request_domain' =>; ['http://xxx.com','https://xxx2.cc'],
注意域名的末尾不能带/ 如果用到上传文件,则需要配合以上nginx一起配置