Help with replacing chars "_" to "." in a $variable

I would appreciate your help

I have to do the following request

Where must be deleted and subdomain_domain_extension_tdl
must be converted to subdomain.domain.extension.tdl

I did this:

    set $subdomain "";

    if ($host ~* "^(.+)\.domain\.com$") {
        set $subdomain $1;

      proxy_pass        http://$subdomain;
      proxy_set_header  X-Real-IP  $remote_addr;

Then i get $subdomain as: subdomain_domain_extension_tdl but i need to
transform it into subdomain.domain.extension.tdl

Could you please help me determining how could i do this? i would like
to serve as proxy in this way:

If you want to get inside any blocked website like be
able to do it throug

Also i know that perhaps sometimes subdomain_domain_extension_tdl could
be domain_extension_tdl,

Thanks in advance!

Posted at Nginx Forum:,221176,221176#msg-221176