ng获取响应体的json里面的字段需要安装第三方模块 ngx_devel_kit的ngx_http_set_misc_module的set_json_var指令 , form-input-nginx-module

location /api {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_intercept_errors on;
proxy_redirect off;
proxy_pass_request_body off;
proxy_pass_request_headers on;

# 使用ngx_http_set_misc模块获取JSON响应体字段
set $json_field "";
set_json_var $json_field "$upstream_http_content_type" "$upstream_http_json_data.key";

# 将JSON响应体字段传递给后端服务器
proxy_set_header X-My-Json-Field $json_field;
}
在这个配置中,set_json_var指令会从上游服务器的响应头中获取content-type字段,如果它的值为application/json,则会从响应体中获取key字段的值。获取到的key字段的值会被赋值给变量$json_field。

然后,可以使用proxy_set_header指令将$json_field传递给后端服务器。在这个例子中,将$json_field赋值给X-My-Json-Field请求头。

需要注意的是,这个示例假设上游服务器返回的是一个包含JSON数据的响应体,且其中包含一个名为key的字段。如果实际情况不同,需要根据实际情况修改set_json_var指令的参数。

另外,需要注意ngx_http_set_misc模块可能会影响nginx的性能,因此在使用之前需要评估其性能影响。

 

!!!上面是获取 响应体的json中的字段值
下面是ng通过 代理接口 做用户验证登录

location /minio {
# 这是一个需要认证的location
auth_request /auth;
proxy_pass http://localhost:9000;
}

location /auth{
interal;
# 发送认证请求到其他系统
auth_request http://10.20.33.12:31018/videoauth/login;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
proxy_set_header X-Original-Method $request_method;

# 如果认证成功,则重定向到需要认证的location
#auth_request_set $auth_cookie $upstream_http_set_cookie;
#add_header Set-Cookie $auth_cookie;
#return 302 /secret;

auth_request_set $auth_response_code $upstream_http_code;
auth_request_set_header X-Auth-Status $auth_response_code;
auth_request_set_header X-Auth-Response $upstream_http_content_type;
auth_request_set $auth_response_body $upstream_http_content_type;

}

location /auth-check {
# 发送认证请求到其他系统,并等待认证响应
internal;
proxy_pass http://其他系统地址/auth-check;
}