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_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?

| improve this question | | | | |
  • On the server, blank.png exists at %nginxroot%/html/tiles/blank.png. From a browser, any request to myhost/tiles/* that results in a 404 not found should return blank.png – GojiraDeMonstah Mar 14 '15 at 16:35
  • Ok. In re-reading your previous comment, I understand you to be saying there is a path problem. If I change the line from error_page 404 = blank.png; to error_page 404 = tiles/blank.png; or the browser redirects to myhost/tiles/sectionals/0/0/tiles/tiles/tiles/[repeated]/…. But if I change it to error_page 404 = /tiles/blank.png; it works. So that's the answer. – GojiraDeMonstah Mar 14 '15 at 16:43

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;
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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