简体   繁体   English

为什么 HTTP OPTIONS 请求在 PHP-FPM 上不起作用?

[英]Why HTTP OPTIONS request doesn't work on PHP-FPM?

When using apache handler instead of FastCGI / PHP-FPM I can use a workaround to response a preflight HTTP request, checking REQUEST_METHOD and returning http code 200.当使用 apache 处理程序而不是 FastCGI / PHP-FPM 时,我可以使用一种解决方法来响应预检 HTTP 请求,检查 REQUEST_METHOD 并返回 http 代码 200。

However using FastCGI handler the same code doesn't work, and chrome fails saying Access-Control-Allow-Origin was found on request.但是,使用 FastCGI 处理程序时,相同的代码不起作用,并且 chrome 无法根据请求找到 Access-Control-Allow-Origin。

Is there a way to make this work?有没有办法使这项工作?

    $http_origin = $_SERVER['HTTP_ORIGIN'];
    header("Access-Control-Allow-Origin: $http_origin");  
    header("Access-Control-Allow-Headers: Content-Type, Origin, Authorization");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS');
    header('Content-Type: application/json');
    
    if ($_SERVER['REQUEST_METHOD'] == "OPTIONS"){
        exit(0);
    } 

Removing:删除:

header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS');

Rearranging to:重新排列为:

$http_origin = $_SERVER['HTTP_ORIGIN'];

header("Access-Control-Allow-Headers: Content-Type, Origin, Authorization, X-Auth-Token");
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Origin: $http_origin");

if ($_SERVER['REQUEST_METHOD'] === "OPTIONS"){
    exit(0);
}

Worked fine.工作得很好。 I had to change the mpm module since I had the issue and today I needed to change.我不得不更改 mpm 模块,因为我遇到了问题,今天我需要更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM