Using image_filter to return both images and non-image files

lrkwz asked:

I’ve configured nginx to image_filter the requests

server {
    listen 80;
    server_name localhost;

    root /usr/share/nginx/html;

    location / {
        index index.html;
        image_filter_buffer 20M;
        image_filter_jpeg_quality 75; # Desired JPG quality
        image_filter_interlace on; # For progressive JPG
        image_filter resize $arg_width $arg_height;
        try_files $uri $uri/ =404;
    }
}

but the location contains BOTH images and other files (such as pdf, html, …).
Of course image_filter returns 415 when requested with a non-image content such as:

curl http://localhost/sample.pdf

How can I have all the files returned without changing the directory structure?

My answer:


Confine the image_filter to a regex location that matches only files with an image extension. For example:

location ~ \.(jpg|gif|png|webp)$ {
    ....
}

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.