Nginx 使用 ngx.location.capture 修改 Authorization 头
在 Nginx 的 ngx.location.capture 中,您可以使用 ngx.req.set_header 方法来删除原始请求的 Authorization 头,并添加新的 Authorization 头。
以下是一个示例代码:
location /proxy {
resolver 8.8.8.8;
set $new_authorization 'Bearer your_new_token';
rewrite ^/proxy/(.*)$ /$1 break;
proxy_pass http://backend$request_uri;
proxy_set_header Host $host;
proxy_set_header Authorization $new_authorization;
header_filter_by_lua_block {
ngx.req.clear_header('Authorization');
ngx.req.set_header('Authorization', ngx.var.new_authorization);
}
}
在这个例子中,我们首先定义了一个新的 Authorization 头,然后使用 ngx.req.clear_header 方法删除原始请求的 Authorization 头。接着,使用 ngx.req.set_header 方法将新的 Authorization 头添加到请求中。
请根据您的实际需求进行相应的修改。
原文地址: https://www.cveoy.top/t/topic/p3Dm 著作权归作者所有。请勿转载和采集!