Nginx Execute Multiple Location Blocks

I have the following nginx location block:

location ^~ /publish {
  allow 127.0.0.1;

  #Allow home
  allow 99.100.101.102;

  deny all;
}

Then this location block to process PHP after:

location ~\.php {
  try_files $uri =404;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_intercept_errors on;
  fastcgi_pass 127.0.0.1:9000;
  include /etc/nginx/fastcgi_params;
}

The problem is that files inside of /publish like execute.php aren't being processed by the fastcgi PHP processor, they simply output the pure PHP code when called from CURL. How do I enforce the security rules defined in the /publish block but also still execute the PHP block if the file ends in .php?


nginx only applies one location block to a request.

Use include if you'd like to share directives between blocks, or a try_files pointing to an @php location if you'd like to point multiple PHP-using location blocks to a single PHP handling location.