NGINX 404 errors

Somewhere out in the wild there’s an NGINX config file that a lot of people are copying (ourselves included though we don’t remember where we found it!), and it’s likely causing some hard to track down 404 errors.

Fortunately we saw it crop up on one of our first NGINX dev boxes.

Within the location block, it’s pretty common to setup another location block that automatically disables logging and sets 30 day expires headers on static assets.  This is best handled like so:

1
2
3
4
location ~* .(jpg|jpeg|gif|css|png|js|ico|eot|svg|ttf|woff)$ {
    access_log        off;
    expires           30d;
}

The problem is that we originally had hastily grabbed this location block from a tutorial

1
2
3
4
location ~* ^.+(jpg|jpeg|gif|css|png|js|ico|eot|svg|ttf|woff)$ {
    access_log        off;
    expires           30d;
}

The difference is subtle, but the latter codeblock improperly matches for ANY url ending in those strings. We have a number of hashed URLs to validate certain requests, and since they are randomly generated, we occasionally saw 404 errors from these requests. It wasn’t until we noticed that one of the random strings ended in “js” that we thought to look at this block.

Leave a comment