Nginx – Intercepting 404 with nginx as reverse proxy for memcached + node.js

http-status-code-404memcachednginxnode.jsreverse-proxy

I'm using nginx as a reverse proxy to look up html content from memcached, if not present, then node.js, if not even there, node.js returns 404.
What I'm trying to do is intercept and deliver a custom page for the 404 returned by node.js

The problem is that nginx doesn't return the correct 404.html but rather it's default one.

So while I can intercept the 404 page, nginx deliveres the default error page, not 404.html

upstream memcached {
    server 127.0.0.1:11211;
}

error_page 404 /404.html;
location /404.html {
    internal;
}

location @memcached {
    ...
    if ($memcached_request = 1) {
        memcached_pass memcached;
        error_page 404 502 504 = @nodejs;
    }

    if ($memcached_request = 0) {
        error_page 404 502 504 = @nodejs;
    }
}

location @nodejs {
    proxy_pass         http://127.0.0.1:7777;
    ...
    error_page 404 /404.html;
    proxy_intercept_errors on;
    proxy_redirect off;
}

location / {
    try_files $uri $uri/ @memcached;
    error_page 403 = @nodejs;
}

Is there a better way to do this "if not in memcached, go to node.js", instead of the error_page 404 = @fallback?
If not, is there a way to catch the 404 from the last backend?

Best Answer

Maybe it doesn't want to redirect to an internal location? What if you try to redirect it to a named location again? I.e. @404?

Else, it may sound like you might have to enable recursive_error_pages for your setup to work.