title: nginx add_header on specific URI to PHP app with front controller
body: <p>I have a pretty standard setup with a symfony2-like app with a front controller, running on nginx 1.10 and Centos7. It all works as expected, blocks where expected etc.</p>
  
  <pre><code>server {
      listen 80;
  
      root /opt/my/code/web;
      index app.php;
      charset utf-8;
  
      location / {
          try_files $uri $uri/ /app.php$is_args$args;
      }
  
      # pass the PHP scripts to php5-fpm
      location ~ ^/app\.php(/|$) {
  
          # problem here
          location ~ ^/recording {
              add_header Content-Type audio/x-wav;
          }
  
          fastcgi_split_path_info ^(.+?\.php)(/?.*)$;
          fastcgi_pass unix:/var/run/php-fpm.sock;
          fastcgi_index app.php;
          include /etc/nginx/fastcgi_params;
          fastcgi_param DOCUMENT_ROOT $realpath_root;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  
          # Prevents URIs that include the front controller. This will 404:
          internal;
      }
  
      # return 404 for all other php files not matching the front controller
      location ~ \.php$ {
          return 404;
      }
  }
  </code></pre>
  
  <p>I have a few issues but the main one is that I want special handling for a URI matching <code>/recording</code> but it still has to go through the front controller. (This is not debatable, it HAS to go through the front controller and modify a response header if the URI matches <code>/recording</code>)</p>
  
  <p>Since <code>try_files</code> redirects to <code>location ~ ^/app\.php(/|$)</code> nginx's <code>$uri</code> parameter used for location matching gets updated to <code>/app.php</code>, so any nested locations won't work.</p>
  
  <p>I cant use <code>add_header</code> outside of the front controller block because any <code>add_header</code> directives get dropped on an internal redirect. </p>
  
  <p>Obviously I can't use <code>location if</code> with <code>add_header</code> either.</p>
  
  <p>This is easy in apache, but the only remote solution I have found uses a third party lua module and the installation docs are a bit thin on that and the thought of compiling that in from source on centos is giving me heart palpitations.</p>