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

标签 nginx openresty

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/

相关文章:

angular - Nginx Angular Docker

nginx - 使用 NGINX 和 Lua 限制每个 IP 的请求频率

postgresql - ^ 和 $ 在 nginx.conf 位置做什么?

ssl - 通过 Letsencrypt SSL 证书的 WebSocket 连接失败

NGINX : Making two asynchronously calls to a backend server

nginx - Openresty:使用lua进行http调用并返回其解析结果

json - openresty 上游 postgres 调用一个函数,它自己返回一个 json

Django + gunicorn + nginx : 502 bad gateway but sometimes only?

wordpress - 我们可以将 nginx 同时用于 WordPress 和 Drupal 吗?

ruby-on-rails - 504 网关超时 nginx/1.4.6 (Ubuntu)