title: nginx rewrite if file exist
body: <p>My script modifies image and returns the result.</p>
  
  <p>I want to process the image only if it exists.</p>
  
  <p>However the following block won't work, nginx just returns the original image.</p>
  
  <pre><code>location ~* \.(jpg|jpeg|png)$ {
      if (-f $uri) {
          rewrite (.*) /imagemagic.php?src=$1 last;
      }
  }
  </code></pre>
  
  <p>I initially used this inside the script to check, however I don't like it sitting in the script file...</p>
  
  <pre><code>if (!file_exists($_GET['src'])) {
      http_response_code(404);
      exit();
  }
  </code></pre>
  
  <p>How am I supposed to do this using nginx settings? Thanks.</p>