【发布时间】:2022-12-21 10:12:36
【问题描述】:
我最近在使用 NGINX 时遇到了一些问题。我需要构建一个 NGINX 反向代理,它在特定 NGINX 变量中获取一些信息并将其发送到授权服务器。不过,在发送到上述 AuthServer 之前,需要将其解析为 HTTP POST 请求的主体。
为了完成这最后一步,我决定编写一个 NJS 函数,它获取此信息并通过子请求将其发送到授权服务器。
然而,有一个小问题:由于 NJS 函数是在 auth_request 位置调用的,它显然无法访问 NGINX 变量。
因此,我尝试通过配置添加的一些 costum 标头(在 auth_request 位置内部和外部)将变量值发送到 NJS 函数。不幸的是,这并没有解决问题。
这是我的 NGINX 配置文件:
location /operation/ {
auth_request /authz;
auth_request_set $example_variable "Plz work variable";
add_header X-Example-Header1 "Plz work header 1";
proxy_set_header X-Example-Header2 "Plz work header 2";
proxy_pass_request_headers on;
proxy_pass http://service;
}
location /authz {
internal;
proxy_pass_header Authorization;
proxy_set_header Authorization $http_authorization;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
proxy_set_header X-Original-METHOD $request_method;
add_header X-Example-Header3 "Plz work header 3";
proxy_set_header X-Example-Header4 "Plz work header 4";
proxy_pass_request_headers on;
js_content auth_engine.authorize_operation;
}
这是 js_content 中使用的 JS 函数的代码:
function authorize_operation(r) {
r.log(JSON.stringify(r.headersIn));
r.log(JSON.stringify(r.variables));
// ...some code which sends data to OPA...
}
authorize_operation中的两行代码都没有打印NGINX配置应该添加的信息。具体来说,第一个 r.log 只打印初始请求的标头(而不是配置添加的标头),第二个不打印任何东西,只打印一些大括号。
有没有什么办法解决这一问题?
【问题讨论】:
标签: nginx nginx-reverse-proxy nginx-config nginx-location njs