I have a situation where I'd like to proxy_pass to a backend server, with the url for the server being dynamic and derived from either a url param value or from a header param value. Not sure that is even possible, still a bit of a novice with NGINX proxies.
I had thought that $arg_name was the generic way to get the value for a query string parameter in NGINX, so if I have a URL like ....?proxy=/proxy1, then $arg_proxy should have the value of "/proxy1" ?
What I am aiming for is something like this:
set $proxy "http:/${arg_proxy}:8042";
location /backend/ {
proxy_pass $proxy;
rewrite /backend(.*) $1 break;
proxy_set_header Host $http_host;
proxy_request_buffering off;
proxy_max_temp_file_size 0;
client_max_body_size 0;
}
I tried that, and a few other variations with settting the variable. It doesn't crash the server, but it is also acting like the value for $arg_proxy is blank, even though it is not, or isn't getting read.
invalid URL prefix in "http:/:8042"
It would be nice to alternatively extract the proxy value from a header param, because there are cases where that would also be helfpul.
Addendum:
I tried something like this using a Cookie:
map $cookie_TEST $proxy_url {
volatile;
default "";
"pacs-1" "pacs-1";
"pacs-2" "pacs-2";
"pacs-3" "pacs-3";
}
upstream pacs-1 {
server pacs-1:8042;
keepalive 32;
keepalive_requests 1024;
keepalive_time 1h;
keepalive_timeout 600s;
}
upstream pacs-2 {
server pacs-2:8042;
keepalive 32;
keepalive_requests 1024;
keepalive_time 1h;
keepalive_timeout 600s;
}
upstream pacs-3 {
server pacs-3:8042;
keepalive 32;
keepalive_requests 1024;
keepalive_time 1h;
keepalive_timeout 600s;
}
But then in the proxy block:
if ($proxy_url = 'pacs-1') {
rewrite /orthanc(.*) $1 break;
proxy_pass http://pacs-1;
}
if ($proxy_url = 'pacs-2') {
rewrite /orthanc(.*) $1 break;
proxy_pass http://pacs-2;
}
if ($proxy_url = 'pacs-3') {
rewrite /orthanc(.*) $1 break;
proxy_pass http://pacs-3;
}
Seems like there should be a way without using if's, which is a bad idea really, but it seems to "work" so far with what I am trying.
CodePudding user response:
Args are uri specific , try using the below example:
location /search {
set $proxxy "www.google.com";
if ($args) {
set $proxxy $arg_proxy;
}
if ($proxxy){
proxy_pass https://$proxxy;
}
proxy_pass https://www.google.com;
}
one can't access access args independent of the request.