I recently used Nginx to serve a few static JSON files as lightweight configuration endpoints. The simplest configuration looks like this:
1 | location ~ ^/myconf/(?<filename>.*)$ { |
The problem is that when the requested file does not exist, Nginx returns its default 404 page. To make this easier for frontend code to handle, I changed the configuration so that missing files return an empty JSON object instead:
1 | location ~ ^/myconf/(?<filename>.*)$ { |
Now, if the file is missing, the response is an empty JSON object.
But there is still another issue: Nginx will log the 404 event in error.log even though the client gets the fallback response.
So the configuration needs one more change to suppress the not-found log:
1 | location ~ ^/myconf/(?<filename>.*)$ { |
That does the trick. Using Nginx’s static file handling for simple lightweight interfaces can be quite convenient.