Can Nginx force multiple location? matches

such as

    location /api {
      add_header "Access-Control-Allow-Origin" "*";
    }
    location ~ [^/]\.php(/|$) {
      fastcgi_pass unix:/dev/shm/php-cgi.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
    }

at this point, if you visit .php under api, there will be no add_header, and you can only rewrite a regular match, which is very disgraceful. Is there a good solution?

Nov.19,2021

  1. add an extra sentence to the regular rule add_header
  2. use include to reference from the same file
  3. (you can write this, but it is not officially recommended) nest the second location block inside the first block, but do more tests to ensure that parsed bug
  4. is not introduced.
Menu