<p>I use k8s to deploy my services.
I hope when I access the ingress host <strong>foo.example.com</strong> and the ingress forward it to <strong>server:8000/proxy/foo</strong>. The subdomain <strong>foo</strong> is dynamic and can be changed to any word. The expected result is as below:</p>
<ul>
<li>foo.example.com -&gt; server:8000/proxy/foo</li>
<li>bar.example.com -&gt; server:8000/proxy/bar</li>
<li>......</li>
</ul>
<p>I knew the ingress host could use wildcards and use ingress-nginx <a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/" rel="nofollow noreferrer">rewrite</a> can rewrite the url path. I use the ingress-nginx controller. The ingress yaml file like as below:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /proxy/${wildcard}/$1 # Could I use the host wildcard here?
name: nginx-forward
spec:
  rules:
  - host: *.example.com # I want to get the subdomain wildcard
    http:
      paths:
      - backend:
        service:
          name: service
        port:
          number: 8080
      path: /(.*)
      pathType: Prefix
  tls: # update this attribute
  - hosts:
    - *.example.com 
    secretName: my-secret
</code></pre>
<p>How could I use k8s ingress or other things to get what I want? Thanks.</p>

zzm
<p>You can use server snippet to get the subdomain:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/server-snippet: |
      server_name     ~^(?&lt;subdomain&gt;\w+)\.example\.com$;
    nginx.ingress.kubernetes.io/rewrite-target: /proxy/$subdomain/$1 
name: nginx-forward
spec:
  rules:
  - http:
      paths:
      - backend:
        service:
          name: service
        port:
          number: 8080
      path: /(.*)
      pathType: Prefix
</code></pre>
