Welcome! Log In Create A New Profile

Advanced

Nginx - How to only allow image requests

May 06, 2021 01:45PM
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?
Subject Author Posted

Nginx - How to only allow image requests

amenjava May 06, 2021 01:45PM

Re: Nginx - How to only allow image requests

Francis Daly May 07, 2021 09:08AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 126
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready