nginx serving alternate location on 404

16,673

Solution 1

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;

   }

Solution 2

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:
16,673

Related videos on Youtube

Steve Bennett
Author by

Steve Bennett

Updated on September 18, 2022

Comments

  • Steve Bennett
    Steve Bennett over 1 year

    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)?
    • Steve Bennett
      Steve Bennett over 10 years
      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
    Steve Bennett over 10 years
    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).
  • that guy from over there
    that guy from over there over 10 years
    ah, understood. nice concept!