如何将nginx配置为仅在内容类型正确的情况下返回来自代理的响应?


location / {
proxy_pass       http://image_server:8000;
}

在正常情况下,代理服务器将返回图像/png。但在其他情况下(例如超出带宽(,它将抛出应用程序/json(使用 200 HTTP(而不是图像。 那么如何只接受来自代理的图像,我想显示一个 500 错误页面,而不是它从代理返回应用程序/json

一个 openresty/lua-ngx-module 解决方案:

location / {
proxy_pass       http://image_server:8000;
header_filter_by_lua_block {
if string.find( ngx.header.content_type, "json" ) then
ngx.exit(500)
end
}
}

location / {
proxy_pass       http://image_server:8000;
header_filter_by_lua_block {
if string.find( ngx.header.content_type, "image/" ) == nil then
ngx.exit(500)
end
}
}

相关内容

最新更新