location / {
proxy_pass http://image_server:8000;
}
正常情况下,代理服务器会返回image/png。但在其他情况下(例如带宽不足),它将抛出 application/json (带有 200 HTTP)而不是图像。 那么如何只接受来自代理的图像,我想显示 500 错误页面而不是从代理返回 application/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
}
}
关于nginx - 如何配置 nginx 仅在内容类型正确的情况下返回来自代理的响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62266308/