0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah. I think it might have actually been working, for the first request, but not for subsequent ones because the query string param isn't there for .css, .js, subsequent requests, although the referrer has it. If it possible to grab something from my session (Using PHP), that would be better actually. Is that possible, I guess via a Cookie ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.