如果内容类型正确,如何将 nginx 配置为仅 return 来自代理的响应?

How to config nginx to only return the response from the proxy in case the content-type is correct?

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

正常情况下,代理服务器会returnimage/png。但在其他情况下(例如带宽不足),它会抛出 application/json(使用 200 HTTP)而不是图像。 那么如何只接受来自代理的图像,我想显示一个 500 错误页面而不是 returning 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
    }
}