make it so multiple matching location blocks are handled

neubert asked:

In my nginx conf.d file I have the following:

  location ~ \.php$ {
    try_files $uri = 404;
    fastcgi_pass unix:/tmp/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;

  location ^~ /dev/ {
    fastcgi_param PHP_VALUE display_errors=On;

My goal was to have it so *.php was parsed by PHP in any directory, including the /dev/ directory and that, within the /dev/ directory, display_errors would be enabled. But it seems that the locations aren’t being ANDed together but rather ORd. So if it matches *.php it parses it as a PHP file unless it’s in the /dev/ directory.

Do I have to be redundant and have the PHP config code duplicated twice or is there a way I can avoid that redundancy?


My answer:

Yes, you have to be redundant. Except in very limited circumstances, location blocks are only parsed once, and the closest match wins.

Though you can reduce the pain of this by removing the common configuration parameters to a separate file and then using include to include it in both places.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.