nginx 和 php 跨域配置

nginx 和 php 跨域配置

如果可以在php里面搞定,尽量不弄nginx的,以免影响迁移 nginx配置

1
2
3
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET,POST,PUT,DELETE,PATCH,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,Authorization'; 

php

1
2
3
4
5
6
7
8
9
// 设置允许其他域名访问
header('Access-Control-Allow-Origin:*');
//header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN'].'');

// 设置允许的响应类型
header('Access-Control-Allow-Methods:POST, GET,OPTIONS, PUT, DELETE');

// 设置允许的响应头
header('Access-Control-Allow-Headers:x-requested-with,content-type,Origin, Accept, Authorization'); 

thinkphp 直接放到 应用的index.php 就可以了

Licensed under CC BY-NC-SA 4.0
最后更新于 2022-09-07 19:56 UTC
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计