DEV Community

Matt Kenefick
Matt Kenefick

Posted on

Using nginx to look for files by server date, e.g. 2022-07-10.json

I ran into an instance recently where I wanted a low cost solution to serving up pregenerated files by date on the server.

We couldn't allow users to fetch on their by query string or similar; the server had to be in charge.

location /example-endpoint {
    set $file "index.json";

    if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})") {
        set $file "$1-$2-$3.json";
    }

    index $file index.json =404;
}
Enter fullscreen mode Exit fullscreen mode

This will look for a file of 2022-07-10.json, default to index.json, and then a 404.

It allows you to setup a lot of pregenerated content on the server that changes per day without the ability to skip ahead.

No additional processing required.

Top comments (0)