Nginx serving alternate location on 404

configurationhttp-status-code-404nginxPROXYrewrite

I'm trying to set up an nginx config as follows: When receiving a request like /tile/SteveCountryVic/1/2/3.png:

  1. Attempt to pass it through to http://localhost:5005/1/2/3.png
  2. If that 404s, pass it to another server as /tile/SteveCountryVic/1/2/3.png

Here's my config which isn't quite working:

server {
   listen 80;
   server_name localhost;   error_log  /tmp/nginx.error.log notice;
   access_log   /tmp/nginx.access.log;
   location /tile/SteveCountryVic/ {
        rewrite_log on;        
        #rewrite ^.*/(\d+)/(\d+)/(\d+).*$ /$1/$2/$3.png break;

        proxy_intercept_errors on;
        error_page 404 = @dynamiccycletour;        
        #proxy_set_header Host $http_host;
        #proxy_pass http://127.0.0.1:5005;
        proxy_redirect /tile/SteveCountryVic/ http://localhost:5005/;

   location @dynamiccycletour {
        rewrite_log on;
        #rewrite ^(\d+)/(\d+)/(\d+).*$ /tile/SteveCountryVic/$1/$2/$3.png break;
        proxy_pass http://115.x.x.x:20008;


   }

   location /tile/ {
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:20008;


        proxy_cache my-cache;
        proxy_cache_valid  200 302  60m;
        proxy_cache_valid  404      1m;
    }
    ...

In this configuration, all requests seem to get redirected to the proxied server, but images are ultimately served. In addition, the error log contains these lines:

2013/09/10 09:44:11 [error] 564#0: *138 open() "/etc/nginx/html/tile/SteveCountryVic/13/7399/5027.png" failed (2: No such file or directory), client: 118.x.x.x, server: localhost, request: "GET /tile/SteveCountryVic/13/7399/5027.png?updated=15 HTTP/1.1", host: "mydomain.org"

If instead of using proxy_redirect, I use rewrite and proxy_pass:

        rewrite ^.*/(\d+)/(\d+)/(\d+).*$ /$1/$2/$3.png break;
        proxy_pass http://127.0.0.1:5005;

Then now I actually see the 404 messages in the browser (ie, they don't get intercepted).

My questions:

  1. What am I doing wrong?
  2. Why on earth is nginx looking for files in /etc/nginx/html/…?
  3. Is there a way to get even more logging information (specifically, to better understand proxy_redirect)?

Best Answer

The alternative version, using rewrite and proxy_pass behaved perfectly - the problem was the other server returning 200's instead of 404's. So for completeness, here is the working config:

server {
   listen 80;
   server_name localhost;
   error_log  /tmp/nginx.error.log notice;
   access_log   /tmp/nginx.access.log;
   location /tile/SteveCountryVic/ {
        rewrite_log on;
        rewrite ^.*/(\d+)/(\d+)/(\d+.*)$ /$1/$2/$3 break;

        proxy_intercept_errors on;
        error_page 404 = @dynamiccycletour;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:5005;
  }

   location @dynamiccycletour {
        rewrite_log on;
        rewrite ^/(\d+)/(\d+)/(\d+.*)$ /tile/SteveCountryVic/$1/$2/$3 break;
        proxy_pass http://115.x.x.x:20008;

   }
Related Topic