Correctly set up hotlink protection for images (solved)

I am trying to set up hotlink protection for images with the following (inside my /etc/nginx/site-available/ server block) but it does not appear to be working correctly:

        location ~* \.(svg|svgz|jpg|jpeg|gif|png|ico|bmp)$ {
            valid_referers blocked;
            if ($invalid_referer) {
            return 403;

(My code is similar to that found here: )

(1) I’ve restarted nginx after changes with no errors. (2) I’ve purged the cache. (3) I use cloudflare but cannot find any obvious conflicts.

Have I done anything obviously wrong here?

As I suspected, my problem is that the location block is already specified elsewhere. See here for solution: