Nginx - How to only allow image requests

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


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;

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: - works (HTTP 200)
  - Doesnt work (HTTP 403)

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

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


Can anyone provide some pointers around it?

Posted at Nginx Forum:,291446,291446#msg-291446

More information about the nginx mailing list