1

I have the following:

location ~ \.php$ {
    try_files $uri =404;

    include fastcgi_params;

    fastcgi_pass unix:/var/run/php-fpm.sock;
    ...
    if ($host = "www.domain.tld") {
        add_header Access-Control-Allow-Origin "https://site.domain.tld";
    }

what is the correct way to change the header so that if the path is contains /api the header then becomes:

add_header Access-Control-Allow-Origin "https://docs.domain.tld";

any advice is much appreciated

1
  • Have you tried my solution? Commented Nov 17, 2018 at 18:24

1 Answer 1

1

You should better create 2 different locations and share common proxy config and include as-needed.

/etc/nginx/proxy.conf

try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php-fpm.sock;
...

Your Host Config File

location ~ \.php$ {
    include /etc/nginx/proxy.conf;
    ...
    if ($host = "www.domain.tld") {
        add_header Access-Control-Allow-Origin "https://site.domain.tld";
    }
}
location ~ .*/api.*.php$ {
    include /etc/nginx/proxy.conf;
    ....
    add_header Access-Control-Allow-Origin "https://docs.domain.tld";
    ....
}

Moreover, this can also be solved by using if statement on the $uri variable. But If Is Evil. That said you should avoid using if statement on the $host it is a bad practice.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.