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

I would appreciate your help

I have to do the following request

http://subdomain_domain_extension_tdl.domain.com/

Where .domain.com 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;
        break;
    }

      resolver 8.8.8.8;
      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 www.facebook.com be
able to do it throug www_facebook_com.domain.com

Also i know that perhaps sometimes subdomain_domain_extension_tdl could
be domain_extension_tdl,

Thanks in advance!
Andrés

Posted at Nginx Forum: