title: Nginx Match Image Filename Based On Part of URL?
body: <p>For local development, I'm trying to serve static content out of a public directory. My images live in <code>app/public/images</code>, and my root is <code>app/public</code>. My images are looked up at <code>/d/my_app_name/images/&lt;image&gt;.png</code>. I'm matching the <code>/d/my_app_name/images</code> location, but trying to figure out how to then extract the file name and have nginx look up the appropriate filename from the images directory? My current settings:</p>
  <pre><code>    root /app/public;
      index index.php;
  
  
      location /d/my_app_name/images/ {
          root /images/; # &lt;--- I guess this rule is making nginx look up a file that matches the entire relative path, rather than just the filename
      }
  </code></pre>
  <p>UPDATE
  I was able to get it to match and look up the filename only from what appears to be the right directory, but I'm still getting a not found error. I'm assuming that the relative path shown in the error is relative to the root, in which case that file/directory should exist.</p>
  <pre><code>    root /app/public;
      index index.php;
  
      location /d/my_app_name/images/ {
           alias /images/;
      } 
  </code></pre>
  <p>When I go to <code>localhost:8380/d/my_app_name/images/my_image.png</code> it is looking up the correct png file in the app/public/images directory, but it's still saying it doesn't exist.</p>
  <p><code>[error] 8#0: *1 open() &quot;/images/my_image.png&quot; failed (2: No such file or directory), client: 192.168.176.1, server: devbox, request: &quot;GET /d/my_app_name/images/my_image.png HTTP/1.1&quot;, host: &quot;localhost:8380&quot;</code></p>
  <p>Images are kept in <code>/app/public/images</code> directory.</p>