title: How do I use nginx variables in a regex pattern while declaring `server_name`
body: <p>When developing on my system, I use <code>server_name ~^(?&lt;subdomain&gt;.+)\.localhost$;</code> to capture the subdomain, however, in production, my reverse proxy is deployed over multiple domains and the domain name is stored in the nginx variable <code>$domain</code>.</p>
  
  <p>How do I do a regex capture while doing string interpolation at the same time.</p>
  
  <p>E.g. instead of <code>server_name ~^(?&lt;subdomain&gt;.+)\.localhost$</code>  how do I do <code>server_name ~^(?&lt;subdomain&gt;.+)\.${domain}$;</code></p>
  
  <p>Actual code:</p>
  
  <pre><code>  server {
      listen       80;
      server_name ~^(?&lt;subdomain&gt;.+)\.localhost$;
  
      location / {
          proxy_pass https://sarahah.com; # get the joke? ;)
          proxy_set_header Host $subdomain.sarahah.com;
      }
  </code></pre>