NGINX:當請求方法為 OPTIONS 且檔案存在時傳回 CORS 標頭,否則傳遞給 PHP-FPM

NGINX:當請求方法為 OPTIONS 且檔案存在時傳回 CORS 標頭,否則傳遞給 PHP-FPM

我有一個 NGINX,後面有一個 PHP-FPM 實例。OPTIONS對檔案系統中存在檔案的路徑的請求應由 NGINX 處理。對於這些請求,NGINX 應傳回Access-Control-*CORS 標頭。OPTIONS不存在文件的請求應傳遞給 PHP-FPM。

邏輯應該是這樣的:

    location / {
        # In this case: Check if file exists
        # - yes: return CORS headers
        # - no: pass request to PHP-FPM
        if ($request_method = 'OPTIONS') {
            # This causes an error
            try_files @cors;
        }

        # Normal request handling for all non-OPTIONS requests:

        # Try to serve file directly, fallback to index.php if file does not exist
        try_files $uri /index.php$is_args$args;
    }

    location @cors {
        if (-f $request_filename) {
            more_set_headers "Access-Control-Allow-Credentials: true";
            more_set_headers "Access-Control-Allow-Origin: example.com";
            more_set_headers 'Access-Control-Allow-Methods: POST, GET, DELETE, PUT, OPTIONS';
            more_set_headers 'Access-Control-Allow-Headers: content-type,ngsw-bypass';
            more_set_headers 'Access-Control-Max-Age: 3600';

            more_set_headers 'Content-Type: text/plain; charset=UTF-8';
            more_set_headers 'Content-Length: 0';

            return 204;
        }

        try_files /index.php$is_args$args;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass localhost:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;

        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param SCRIPT_FILENAME /var/www/public/index.php;
        fastcgi_param DOCUMENT_ROOT /var/www/public;
        fastcgi_param HTTPS $fastcgi_param_https;

        # Prevents URIs that include the front controller. This will 404:
        # http://domain.tld/index.php/some-path
        # Remove the internal directive to allow URIs like this
        internal;
    }

但這不起作用。由於語句try_files內不允許if([emerg] 1#1:此處不允許「try_files」指令)。

答案1

您可以使用該error_page指令來處理檔案系統中存在檔案的路徑的 OPTIONS 請求。

location / {
    # Try to serve file directly, fallback to index.php if file does not exist
    try_files $uri /index.php$is_args$args;

    # Handle OPTIONS requests for paths for which a file exists in the file system
    if ($request_method = 'OPTIONS') {
        if (-f $request_filename) {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain; charset=utf-8';
            add_header 'Content-Length' 0;
            return 204;
        }
    }
}

# Pass all other requests to PHP-FPM
location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

try_files指令用於直接提供文件,index.php如果文件不存在則回退。此if區塊檢查請求方法是否為 OPTIONS 以及請求的路徑中是否存在檔案。如果這兩個條件都成立,NGINX 會將必要的 CORS 標頭加入到回應中,並傳回 204 No Content 狀態碼。

相關內容