How block access to original JPG files on WordPress with nginx

Licenced under CC-BY-4.0, created by Peter Molnar (), published at to canonical URL https://petermolnar.net/how-block-access-to-original-jpg-files-on-wordpress-with-nginx/ with keywords #security #nginx #WordPress

Block access to non-resized JPG files. Use it with caution.

WARNING: this will block access to all original JPGs, not only the large, resized ones. Use this with caution.

location ~ "^/files/(?:(?!.*-[0-9]{2,4}x[0-9]{2,4}).)*\.jpe?g$" {
    rewrite ^/files(.*) /wp-content/files$1 break;
allow 127.0.0.1;
    deny all;
}

location ~ "^/(?:(?!cache).*?)/(?:(?!.*-[0-9]{2,4}x[0-9]{2,4}).)*\.jpe?g$" {
allow 127.0.0.1;
allow 192.168.42.11;
deny all;
}