Nginx: too many redirects trying to return a specific 404 image?

GojiraDeMonstah asked:

I’m using nginx v1.6.2 to serve both dynamic nodeJS and static file content for a web-based application.

Within a certain location (that happens to be serving static png files only), I want any not found image requests to return one specific placeholder image, “blank.png”.

For example, the file at path %nginxroot%/html/tiles/sectionals/0/0/0.png does exist, so the URL http://myhost/tiles/sectionals/0/0/0.png should return the image file requested. But the file at path %nginxroot%/html/tiles/sectionals/0/0/99.png does not exist, so request http://myhost/tiles/sectionals/0/0/99.png should return the file at %nginxroot%/html/tiles/blank.png.

Here is my (edited for brevity) nginx.conf:

events {
   worker_connections  4096;
}

http {

    include             mime.types;
    default_type        application/octet-stream;
    sendfile            on;
    keepalive_timeout   20;

    gzip                on;
    gzip_comp_level     6;
    gzip_vary           on;
    gzip_min_length     1000;
    gzip_proxied        any;
    gzip_types          text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_buffers        16 8k;

    server {

        listen 80;
        server_name     *.myhost.com;

        root            html;

        #dynamic content at root url
        location / {
            proxy_set_header    X-Real-IP            $remote_addr;
            proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header    X-Forwarded-Proto $scheme;
            proxy_set_header    Host                   $http_host;
            proxy_set_header    X-NginX-Proxy    true;
            proxy_set_header    Connection "";
            proxy_http_version  1.1;
            proxy_pass          http://127.0.0.1:9000;
            proxy_redirect      off;
            rewrite             ^/auth/(.*)$ /$1 break;
        }

        # tiles are all static .png files
        location /tiles/ {
            proxy_intercept_errors on;
            error_page 404 = blank.png;
        }

    }
}

The behavior I’m seeing with this configuration is that if the file exists, it works, if the file doesn’t exist the browser tells me I have a redirect loop and the console shows GET http://myhost/tiles/sectionals/0/0/blank.png net::ERR_TOO_MANY_REDIRECTS.

I’ve tried solutions that might account for a pathing issue, like error_page 404 = ./blank.png, turing off or omitting proxy_intercept_errors, putting a blank.png in both html and tiles directories, and I can’t seem to get it to work – with one exception, and that is to put a blank.png in every single directory where an image might be requested. That seems like a silly workaround however.

So I understand that nginx isn’t able to find blank.png and as such is redirecting infinitely (or beyond some threshold), but what should the .conf file have for that location and/or where should the blank.png be located?

My answer:


You need to specify the absolute path to the document you want to serve. Otherwise it is treated as a path relative to the URL that was originally requested.

error_page 404 = /tiles/blank.png;

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.