Loading... <p>在站点的配置文件中加入</p><pre><code>location / { add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods GET,POST,OPTIONS; add_header Access-Control-Allow-Credentials true; add_header Access-Control-Allow-Headers DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type; add_header Access-Control-Max-Age 1728000; } </code></pre><p>即可解决跨域,缺点是所有域名都可以请求</p><p>使用下面的代码即可配置允许的域名请求</p><pre><code>location / { # 检查域名后缀 if ($http_origin ~ \.test\.com) { add_header Access-Control-Allow-Origin $http_origin; add_header Access-Control-Allow-Methods GET,POST,OPTIONS; add_header Access-Control-Allow-Credentials true; add_header Access-Control-Allow-Headers DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type; add_header Access-Control-Max-Age 1728000; } if ($request_method = OPTIONS) { add_header Access-Control-Allow-Origin $http_origin; add_header Access-Control-Allow-Methods GET,POST,OPTIONS; add_header Access-Control-Allow-Credentials true; add_header Access-Control-Allow-Headers DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type; add_header Access-Control-Max-Age 1728000; return 204; } }</code></pre> 猜你想看 每日一学:PHP 中的array_change_key_case函数详解 祝大家中秋节快乐!给大家送上中秋节小动画。 axios封装示例 使用shell脚本实现服务器CPU负载过高自动推送 Typecho更换字体插件FontLibs Dell 服务器风扇调速指南:让你的服务器更安静 Linux 系统安装部署 Redis 完全指南 Go基本数据类型 每日一学:PHP 中的array_replace_recursive函数详解 每日一学:PHP 中的array_fill函数详解 最后修改:2022 年 06 月 05 日 © 允许规范转载 赞 0 如果觉得我的文章对你有用,请随意赞赏