<p>i am trying to make a dynamic proxy_pass with nginx, doing something like that: </p>

<ul>
<li>Requests to foo.mywebsite.com are forwarded to service with foo name</li>
<li>Requests to bar.mywebsite.com are forwarded to service with bar name</li>
</ul>

<p>my nginx.conf works with static values, but with regex, works for about 5 minutes and then errors start </p>

<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config-dns-file
data:
  nginx.conf: |
    server {
      listen 80;
      server_name ~^(?&lt;subdomain&gt;.*?)\.;
      resolver kube-dns.kube-system.svc.cluster.local valid=5s;

      location /healthz {
        return 200;
      }

      location / {
        proxy_pass http://$subdomain.default.svc.cluster.local; 
      }
    }
</code></pre>

<p>my pod gets the service ip instead of the name, here are the logs</p>

<blockquote>
  <p>2019/11/11 22:30:40 [error] 6#6: <em>163 10.default.svc.cluster.local
  could not be resolved (3: Host not found), client: 10.142.0.34,
  server: ~^(?.</em>?)., request: "GET / HTTP/1.1", host:
  "10.142.0.34"</p>
</blockquote>

<p><strong>10. is the beginning of ip to the point</strong>.
I don't know what is going wrong, can anyone help me with this, thank you!!</p>

Bruno Luis
<p><strong>FIX WITH THIS</strong> </p>

<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config-dns-file
data:
  nginx.conf: |
    server {
      listen 80;
      server_name ~^(?&lt;subdomain&gt;.*?)\.;
      resolver kube-dns.kube-system.svc.cluster.local valid=5s;

      location /healthz {
        return 200;
      }

      location / {
        proxy_set_header Host $host
        proxy_pass http://$subdomain.default.svc.cluster.local; 
      }
    }
</code></pre>
