gpt4 book ai didi

http - 在 Nginx(和 lua)中提供大于最小文件大小的图像

转载 作者:可可西里 更新时间:2023-11-01 16:42:37 27 4
gpt4 key购买 nike

我正在尝试检查请求的文件是否超过特定大小(比如 250 字节),如果是,则提供文件,否则提供默认图像。

但是,我得到了意想不到的结果。检查正文长度的行似乎没有得到尊重,并且 no_image_small.png 得到了服务。 res.status == 200 似乎有效,不过:

  • 请求图片大小:17字节
  • no_image_small.png 大小:3030 字节

nginx错误日志:

2014/12/01 11:52:57 [error] 6033#0: *1 subrequests cycle while processing "/images/blue_image_small.jpg", client: 127.0.0.1, server: images.dev, request: "GET /images/blue_image_small.jpg HTTP/1.1", subrequest: "/images/blue_image_small.jpg", host: "images.dev:8080"
2014/12/01 11:52:57 [error] 6033#0: *1 lua entry thread aborted: runtime error: rewrite_by_lua:2: failed to issue subrequest: -1
stack traceback:
coroutine 0:
[C]: in function 'capture'
rewrite_by_lua:2: in function <rewrite_by_lua:1>, client: 127.0.0.1, server: images.dev, request: "GET /images/blue_image_small.jpg HTTP/1.1", subrequest: "/images/blue_image_small.jpg", host: "images.dev:8080"

我的 nginx 配置:

server {
listen 8080;
server_name images.dev www.images.dev;
root /home/syz/dev/images/;


location ~* ^/images/(.*_small.) {
rewrite_by_lua '
local res = ngx.location.capture(ngx.var.uri)
if res.status == 200 then
local blength = string.len(res.body)
if blength > 250 then
ngx.say(res.body)
ngx.exit(ngx.OK)
else
ngx.redirect("/images/no_image.small.png")
end
else
ngx.redirect("/images/no_image_small.png")
end
';
}
}

最佳答案

您可以读取验证中的 Content-Length header (速度更快),而不是捕获和读取响应正文长度。

我在生产中有类似的验证工作,有些像:

location ~*/img/.*\.(png|jpg|jpeg) {            
set $max_image_size 15000;
# default redirect if requested image too big
error_page 412 = @default_image_loc;

#serving the content
root /local_server/root/;

#validating response
header_filter_by_lua '
local image_size = tonumber(ngx.header.content_length)
if image_size > tonumber(ngx.var.max_image_size) then
ngx.log(ngx.NOTICE, string.format("invalid image size <%s>" , image_size))
ngx.exit(412)
end
';
}

location @default_image_loc {
try_files "/img/default_image.png" @none;
}

访问日志将保持干净,因为 412 跳转是内部的。

关于http - 在 Nginx(和 lua)中提供大于最小文件大小的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27226386/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com