nginx: handling 404 with error_page

Posted by ytw on Server Fault See other posts from Server Fault or by ytw
Published on 2012-12-15T02:19:30Z Indexed on 2012/12/17 23:05 UTC
Read the original article Hit count: 147

Filed under:

Originally, I have something like this in the nginx.conf file.

location ^~ /test_api {
    types   { application/json json; }
    root    /usr/local/www/data;

    rewrite      "/test_api/(.*)"   /api_response/test_api_$1.json            break;
    error_page   404                /api_response/unknown_request.json;
}

When a requested resource is not found locally, unknown_request.json (default response) is returned correctly.

Then I had to change the rewrite to point to a remote server as follows:

    rewrite      "/test_api/(.*)"   $scheme://www.somedomain.com/test_api_$1  break;

It doesn't return unknown_request.json (default response) anymore even though the remote server returns a 404.

Is there a way to continue to return unknown_request.json to the client when the remote server returns a 404 assuming the remote server can't be changed to return unknown_request.json?

Thanks very much.

© Server Fault or respective owner

Related posts about nginx