假設我們有以下配置($branch
在此片段之前設置,但我猜它不相關):
# If the request origin ends in .domain.com
set $allowed_origin "";
if ($http_origin ~* ^(.+\.)?domain\.com$) {
set $allowed_origin $http_origin;
}
add_header Access-Control-Allow-Origin "$allowed_origin" always;
add_header Access-Control-Allow-Credentials "true" always;
add_header Access-Control-Allow-Headers "Accept, Authorization, Content-Type, Cookie, Some-Custom-Header, Another-Custom-Header, X-And-Another-One" always;
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS" always;
root /var/path/www/$branch/www;
index index.php index.html;
error_page 404 = /notfound.php;
location ~ ^/v\d+/ {
root /var/path/www/$branch/www;
try_files $uri_lower $uri_lower/ /some.php?$query_string;
}
location / {
root /var/path/www/$branch/www;
try_files $uri_lower $uri_lower/ =404;
}
location ~ \.php$ {
try_files $uri_lower $uri_lower/ $uri $uri/ =404;
include fastcgi-params;
}
對於以下請求,這工作正常(返回 200):
OPTIONS https://some.domain.com/v1/graphql HTTP/1.1
Host: some.domain.com
Origin: https://another.domain.com
但是,當我只是在第一個中添加一個空if
(沒有add_header
修改,只是一個空if
)時location
:
location ~ ^/v\d+/ {
if ($http_origin ~* ^(.+\.)?domain\.com$) {
}
root /var/path/www/$branch/www;
try_files $uri_lower $uri_lower/ /some.php?$query_string;
}
它開始響應
HTTP/1.1 405 Not Allowed
有誰知道為什麼以及如何在這裡添加 if 條件。如果僅針對允許的來源設定某些 CORS,我可能想在其中操作標頭。