Score:0

How to get variable's value in split_clients NGINX

pl flag

I'm using [split_clients] and it works perfectly in most cases.

Ref: https://nginx.org/en/docs/http/ngx_http_split_clients_module.html

split_clients returns a literally string for further use.

But now, I need returned string contains a variable, like this:

http context:

split_clients "${remote_addr}${http_user_agent}${date_gmt}" $my_variable {
    20%     https://example.com/fixed_string/another_fixed_string;
    *       https://example.com/$1/another_fixed_string;
}

server context:

location ~ ^/abc/(.*) {
   rewrite ^/abc/(.*) $my_variable redirect;
}

When I visit https://example.org/abc/something, it redirect to https://example.com/$1/another_fixed_string with $1 is a literally string in URL.

My expectation is $1 acts as a variable, and its value is something, then redirect to https://example.com/something/another_fixed_string

How to implement it?

pl flag
Is there any way to replace in string, something like `str_replace("$1", $1, $my_variable)`?. This would work if it's feasible.
Gerard H. Pille avatar
in flag
Combine split_clients with a map?
pl flag
@GerardH.Pille thank you, it works. You save my day!
Score:0
pl flag

As @Gerard H. Pille suggested, using map can solve this issue.

http context:

split_clients "${remote_addr}${http_user_agent}${date_gmt}" $my_variable {
    20%     0;
    *       1;
}

map $my_variable $my_url {
   #default you can also set the default value

   0     https://example.com/fixed_string/another_fixed_string;
   1     https://example.com/$1/another_fixed_string;
}

server context:

location ~ ^/abc/(.*) {
   rewrite ^/abc/(.*) $my_url redirect;
}
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.