Apache迁移服务器Nginx,出现跨域问题

发布于 2022-09-11 20:30:44 字数 222 浏览 14 评论 0

clipboard.png

clipboard.png

可能是跨域问题,post请求 服务器变化导致,get接口没问题

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待我真够勒 2022-09-18 20:30:44

有两种方式:
1代码解决,使用cors

        $cors_hosts = ["http://game.a.com","https://game.b.com"];
        $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
        if(in_array($origin,$cors_hosts)){
            header('Access-Control-Allow-Origin', $origin);
        }
        header('Access-Control-Allow-Methods', ['GET, POST, OPTIONS']);
        header('Access-Control-Allow-Credentials', 'true');
        header('Access-Control-Allow-Headers','DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization');
        if($this->request->method() == 'OPTIONS'){
            header('Access-Control-Max-Age',1728000);
            header('Content-Type','text/plain; charset=utf-8');
            header('Content-Length',0);
            header('HTTP/1.1 204 No Content');
        }

2nginx配置

map $http_origin $corsHost {
default 0;
"~http://game.a.com" http://game.a.com;
"~https://game.b.com" https://game.b.com;
}

location ~ .php$ {
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Origin $corsHost;
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';
add_header Access-Control-Allow-Credentials true;
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文