title: nginx; return response code if a file exists by using try_files only
body: <p>Since <a href="http://wiki.nginx.org/IfIsEvil" rel="nofollow noreferrer">IfIsEvil</a> I've been trying to set up a configuration using the directive <code>try_files</code> only so that a maintenance page is displayed together with the response code 503, for any URI without exception, i.e. including php pages, if a maintenance file exist.</p>
  
  <p>There are two problems with my configuration:</p>
  
  <ol>
  <li>The maintenance page does not display for php URIs.</li>
  <li>The response code 503 is not returned if the maintenance.html file exists.</li>
  </ol>
  
  <p>I've seen similar questions <a href="https://stackoverflow.com/q/5600448/1257965">[1]</a>,<a href="http://forum.nginx.org/read.php?2,226232" rel="nofollow noreferrer">[2]</a> but none with a solution that uses <code>try_files</code> only (as opposed the using the <code>if</code> directive) and that unconditionally serves a maintenance page with the response code 503, if the corresponding file is present. Is such a solution possible?</p>
  
  <p>Below is my current non-working conf file. It doesn't contain a 503 response code setting because I don't understand where it's supposed to go in order for it to work as described above.</p>
  
  <pre><code>worker_processes            1;
  
  error_log                   /var/log/nginx/error.log debug;
  
  events {
      worker_connections      1024;
  }
  
  http {
      include                 mime.types;
      default_type            application/octet-stream;
  
      index                   index.php index.html index.htm;
  
      server {
          listen                  80;
          server_name             rpi;
          root                    /www;
  
          location / {
              try_files           /maintenance.html $uri $uri/ /index.php?$args;
  
              # pass the PHP scripts to FastCGI server
              location ~ \.php$ {
                  try_files           $uri =404;
                  fastcgi_pass        unix:/run/php-fpm/php-fpm.sock;
                  fastcgi_index       index.php;
                  include             fastcgi.conf;
              }
          }
      }
  }
  </code></pre>
  
  <p>I guess that my question could alternatively be phrased like this: Can <code>try_files</code> be made to work as an <code>if</code> control structure? If not by itself, can it, with the above goal, be combined with other directives to act as such, excluding the <code>if</code> directive?</p>
  
  <p><strong>edit:</strong> Below is a solution using <code>if</code> that I'm currently using by including it in the server section:</p>
  
  <pre><code>error_page              503 @maintenance;
  
  if (-f $document_root/maintenance.html) {
          return          503;
  }
  
  location @maintenance {
          try_files       /maintenance.html =404;
  }
  </code></pre>