Nginx - How to only allow image requests

amenjava nginx-forum at forum.nginx.org
Thu May 6 17:45:39 UTC 2021


Hello,

I am setting up a proxy that accepts only image requests and sends them to
upstream (internet).

Sample config:

#  Limit requests to only images
     default_type image/webp;
     if ($request_uri !~* ^.+\.(jpg|jpeg|gif|css|png|html|htm|ico|xml|svg) )
{
       return 403;
       break;
     }

The problem is there will be images without extension. How do we tackle
that? I did try with Lua but didn't help

for eg: https://test.com/shark.png - works (HTTP 200)
           https://test.com/shark - Doesnt work (HTTP 403)

# Testing (this is just for testing If the logic works, but doesn't seem
to)

   header_filter_by_lua '
    local val = ngx.header["testheader"]
     if val then
         if (val ~= "img") or (val ~= "image") then
             return ngx.exit(400)
           end
    end

---

Can anyone provide some pointers around it?

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,291446,291446#msg-291446



More information about the nginx mailing list