V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
cy97cool
V2EX  ›  NGINX

原生 nginx 反代后端的时候能不能把 http status_code 转为一个 header,一律返回 200?

  •  
  •   cy97cool · 2019-09-26 21:18:15 +08:00 · 2884 次点击
    这是一个创建于 1645 天前的主题,其中的信息可能已经有所发展或是发生改变。

    需求:nginx 的下游会检查 http 状态码,不是 200 就不返回,这一层我无法控制

    所以需要在 nginx 这一层把状态码都改为 200,同时把真正的 status code 放入到一个自定义 header 里


    目前已经能实现把特定的 header 重命名 比如 Access-Control-Allow-Headers

            proxy_hide_header Access-Control-Allow-Headers;
            add_header Access-Control-Allow-Headers "*" always;
            add_header real_acah "$upstream_http_access_control_allow_headers" always;
    

    注意到 add_header 用到的 always 否则在 404 等页面就不能添加

    2 条回复    2019-09-26 22:02:44 +08:00
    autogen
        1
    autogen  
       2019-09-26 21:52:11 +08:00   ❤️ 1
    location /dealwith_503{
    set $ret_body '{"code": "V00006","msg": "操作太频繁了,请坐下来喝杯茶。"}';
    if ( $arg_callback != "" )
    {
    return 200 'try{$arg_callback($ret_body)}catch(e){}';
    }
    return 200 $ret_body;
    }
    ————————————————
    版权声明:本文为 CSDN 博主「 streamcc 」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接: https://blog.csdn.net/streamcc/article/details/50587861
    cy97cool
        2
    cy97cool  
    OP
       2019-09-26 22:02:44 +08:00
    @autogen 你这样是用 error_page 和 return 200 "content" 弄丢了返回的内容,我不需要改写内容,原样返回即可
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3009 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 15:02 · PVG 23:02 · LAX 08:02 · JFK 11:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.