title: How can i pass subdomain as proxy_pass value in nginx?
body: <p>i am currently in situation where i need to get/catch sub-domain and pass that sub-domain value to proxy_pass in Nginx config.</p>
  
  <p>e.g.</p>
  
  <p>if user enters</p>
  
  <pre><code>http://google.com.mydomain.com
  </code></pre>
  
  <p>then it should do proxy pass as</p>
  
  <pre><code>proxy_pass http://www.google.com/;
  </code></pre>
  
  <p>in above example <code>google.com</code> is <code>sub-domain</code></p>
  
  <p>can this be doable ?
  How can i achieve something like that in nginx ?</p>
  
  <p>currently i am using configuration where sub-domain values are hard-coded in config files , but there are many sub-domains , so i need to do it like this way, but don't know correct syntax.</p>
  
  <pre><code>server {
      listen       80;
      server_name  subdomain.domain.com;
      charset utf-8;
  
      location / {
        proxy_pass http://www.subdomain/;
      }
  }
  </code></pre>
  
  <p>I am using * as A record to redirect all sub-domains to my web-host, i.e. wildcard DNS.</p>
  
  <p>update: </p>
  
  <p>i have found code snippet from <a href="https://stackoverflow.com/a/22774520/1642018">https://stackoverflow.com/a/22774520/1642018</a></p>
  
  <pre><code>server {          
      listen       80;                                               
    # this matches every subdomain of domain.
    server_name .domain.com;                                           
  
    location / {                                                   
  
      set $subdomain "";
  
      if ($host ~* "^(.+)\.domain.com$") {                             
        set $subdomain $1;                                         
      }                                                            
  
      proxy_pass http://$subdomain;   
  
    }                                                              
  } 
  </code></pre>
  
  <p>but the request is showing my default page which is in my default web root.</p>