Score:1

Sending selective args from Nginx proxy to the upstreams through proxy_pass

sb flag

I have a Nginx proxy configured for a Minio S3 object storage server.

Can I remove some of the query params (more than one agrs) when I'm passing a request to the upstream (Minio) and keep the rest of args?

for example: this is the request that received by Nginx:

/my-private-bucket/my-image.jpeg?X-Amz-Algorithm=AWS4-HMAC-SHA256&w=320&X-Amz-Date=20211218T231908Z&h=200

I need to transform the above link to the below link (i.e. removing the extra args: w=320 and h=200) and then use proxy_pass:

/my-private-bucket/my-image.jpeg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20211218T231908Z

Of course I can use multiple IF statements in the location block: Here is a sample or this one. but I think there should be something more rational. Something like using regex and sed in bash:

echo [mentioned uri] | sed -E 's/&?[wh]=[0-9]*//g'

...or even using map blocks in Nginx:

map $args $polished-args
{
    default $args
    ~&?[wh]=[0-9]* ((something magical))
}

Any ideas?

Score:0
us flag

If you can work the other way around, that is, pass a known list of parameters, you can use:

proxy_pass http://backend.example.com/path?param1=$arg_param1&param2=$arg_param2

If the argument order of dropped keys stays always the same, then you can use map:

map $args $cleaned_args {
    default $args;
    ^(.*)[?&]w=[0-9]+(.*)&h=[0-9]+(.*)$ $1$2$3;
    ^(.*)[?&]h=[0-9]+(.*)&w=[0-9]+(.*)$ $1$2$3;
}

The regular expression captures everything before &w or ?w, then everything before &h and the rest into capture groups. The second option does the same for opposite parameter ordering.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.