Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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)?
share|improve this question
    
Sigh, I think I've found it. The local server is actually returning code 200 even when it can't find an image. –  Steve Bennett Sep 10 '13 at 0:31
add comment

2 Answers

first thing you haven't set your root - directive correctly -> thats why you get a 404 -> thats why all requests are redirected to your @dynamiccycletour (openstreetmap?)

btw, whats the difference between /tile/ and /tile/SteveCountryVic/ ?

so we need a little cleanup here first:

server {
   ....
   # define where to find files 
   # be sure to have it like /path/to/tile
   root /path/to/tiles/;


   location /tile/SteveCountryVic/ {

       # if file not found -> remote server
       try_files $uri @dynamiccycletour

        rewrite_log on;        
        # this should cover /1/2/3.png. no?
        rewrite /tile/SteveCountryVic/(.*).png$ /$1.png break;

        # i'm not sure this will match due the the rewrite
        proxy_redirect /tile/SteveCountryVic/ http://localhost:5005/;


   location @dynamiccycletour {
        rewrite_log on;

        # this should cover /1/2/3.png. no?
        rewrite /tile/SteveCountryVic/(.*).png$ /$1.png break;
        proxy_pass http://115.x.x.x:20008;


   }

 }
share|improve this answer
    
Thanks - useful comments. The extra "/tile/" rule is for other map styles which are rendered on this server (I left this unnecessary detail out). In this instance though, I don't really want a "root" - for most requests, there isn't a directory that I want to map onto exactly - they need to go through a separate server (either the local one on port 5005, or the remote one on port 20008). –  Steve Bennett Sep 10 '13 at 8:38
    
ah, understood. nice concept! –  that guy from over there Sep 10 '13 at 10:50
add comment
up vote 0 down vote accepted

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;

   }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.