Hello, I'm trying to capture a header using auth_request_set and handling the request from a js_content. When I request /_validate manually I receive the Header normally, but when it is requested by Auth_request I cannot set $customvar.
I used as a reference the https://github.com/xeioex/njs-examples/blob/master/conf/http/complex_redirects.conf#L32, but i can't figure out what i've been doing wrong.
Nginx.conf:
js_import main from script.js;
server {
location = /_validate {
internal;
js_content main.GetHeader;
}
location = / {
auth_request /_validate;
auth_request_set $customvar $upstream_http_xcustom;
add_header 'test' "$customvar";
proxy_pass https://backend;
}
script.js:
function GetHeader(r) {
r.headersOut['xcustom'] = ["12345"]
r.return(200);
}
export default {GetHeader};
nginx version: nginx/1.19.10
njs: 0.5.3
@rainerhenrichsen
the problem is with add_header, it adds a header to a response.
But, it seems you want to add a header for a proxied request, right?
For this, you need proxy_set_header directive.
Hi @xeioex,
add_header is for debugging purposes only, even using proxy_set_header I can't capture the xcustom header
@rainerhenrichsen
$upstream_http_* is only for proxy_pass locations, replace it with $sent_http_* (as in the example)
@rainerhenrichsen
Hi!
It's not clear where you want to capture that header, but take a look:
function success_and_header(r) {
r.headersOut['X-Test'] = '12345';
r.return(200);
}
```nginx
location = /auth2 {
js_content test.success_and_header;
}
location /secure2 {
auth_request /auth2;
auth_request_set $check "this was sent from auth_request: $sent_http_x_test";
add_header X-Test-Some "$check and goes to client";
proxy_set_header X-Test-Some "$check and goes to backend";
proxy_pass http://127.0.0.1:10000/content;
}
location /content {
return 200 "$http_x_test_some";
}
```shell
$ curl -v localhost:10000/secure2
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 10000 (#0)
> GET /secure2 HTTP/1.1
> Host: localhost:10000
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.19.10
< Date: Fri, 16 Apr 2021 21:11:38 GMT
< Content-Type: application/octet-stream
< Content-Length: 58
< Connection: keep-alive
< X-Test-Some: this was sent from auth_request: 12345 and goes to client
<
* Connection #0 to host localhost left intact
this was sent from auth_request: 12345 and goes to backend
Hi @xeioex,
Tks for support, problem solved with $sent_http_*.