Assume we have the following config ($branch
is set before this snippet, but it's not related, I guess):
# 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;
}
This works fine (returns 200) for the following request:
OPTIONS https://some.domain.com/v1/graphql HTTP/1.1
Host: some.domain.com
Origin: https://another.domain.com
However, when I just add an empty if
(no add_header
modifications, just an empty if
) into the first location
making it:
location ~ ^/v\d+/ {
if ($http_origin ~* ^(.+\.)?domain\.com$) {
}
root /var/path/www/$branch/www;
try_files $uri_lower $uri_lower/ /some.php?$query_string;
}
it starts responding with
HTTP/1.1 405 Not Allowed
Does anyone know why and how I can add an if condition here. Potentially, I'd like to manipulate headers inside this if setting certain CORS one only for allowed origin.