nginx: handling 404 with error_page

ytw asked:

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.

My answer:


You’ve already redirected the request to the remote site; you have no further control over it. The browser will contact the remote site and get whatever response it gets.


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.