title: if conditions break try_files in nginx configuration
body: <p>I have a simple <code>location</code> block in my nginx config which matches static files for my website. What I want to do, is to check if the file exists using <code>try_files</code>, and if it doesn't, redirect to a URL (in this case specified in the <code>@cdn</code> location block). I also want to set some CORS headers.</p>
  
  <p>Below is the relevant configuration.</p>
  
  <pre><code>location ~* \.(css|js|jpe?g|png|gif|otf|eot|svg|ttf|woff|woff2|xml|json)$ {
      if ($request_method = 'OPTIONS') {
          add_header 'Access-Control-Allow-Origin' "$http_origin";
          add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
          add_header 'Access-Control-Max-Age' 1728000;
          add_header 'Content-Type' 'text/plain charset=UTF-8';
          add_header 'Content-Length' 0;
  
          return 204;
      }
  
      if ($request_method = 'POST') {
          add_header 'Access-Control-Allow-Origin' "$http_origin";
          add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
      }
  
      if ($request_method = 'GET') {
          add_header 'Access-Control-Allow-Origin' "$http_origin";
          add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
      }
  
      try_files $uri @cdn;
  }
  
  location @cdn {
      return 301 https://example.com$request_uri;
  }
  </code></pre>
  
  <p>The problem is that I get a 404 response if the file does not exist, instead of a 301 redirect. The configuration worked/works fine before adding the CORS headers. If I remove the handling of the headers, everything works as intended, and I get a 301 response back.</p>
  
  <p>Now I have done a bit of reading about why the if directive is bad and should be avoided, but I still don't know why it breaks my configuration. If I understood correctly, it has something to do with either <code>if</code> or <code>add_header</code> being part of a rewrite module or something like that, and I guess that conflicts with <code>try_files</code>. Perhaps I am not accurate here, but either way I am not sure how to fix it.</p>
  
  <p>Why does the presence of <code>if</code> and/or <code>add_header</code> make nginx give me a 404 instead of a 301 when a file could not be found, and how do I fix it?  Thanks in advance!</p>