title: How do I host multiple services using subdirectories with nginx-ingress?
body: <p><strong>Problem</strong></p>
  
  <p>I would like to host multiple services on a single domain name under different paths. The problem is that I'm unable to get request path rewriting working using <code>nginx-ingress</code>.</p>
  
  <p><strong>What I've tried</strong></p>
  
  <p>I've installed nginx-ingress using <a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-an-nginx-ingress-on-digitalocean-kubernetes-using-helm" rel="nofollow noreferrer">these instructions</a>:</p>
  
  <pre class="lang-sh prettyprint-override"><code>helm install stable/nginx-ingress --name nginx-ingress --set controller.publishService.enabled=true
  </code></pre>
  
  <pre><code>CHART                   APP VERSION
  nginx-ingress-0.3.7     1.5.7
  </code></pre>
  
  <p>The example works great with hostname based backends:</p>
  
  <pre><code>apiVersion: extensions/v1beta1
  kind: Ingress
  metadata:
    name: hello-kubernetes-ingress
    annotations:
      kubernetes.io/ingress.class: nginx
  spec:
    rules:
    - host: first.testdomain.com
      http:
        paths:
        - backend:
            serviceName: hello-kubernetes-first
            servicePort: 80
  </code></pre>
  
  <p>However, I can't get path rewriting to work. This version redirects requests to the <code>hello-kubernetes-first</code> service, but doesn't do the path rewrite so I get a 404 error from that service because it's looking for the /foo directory within that service (which doesn't exist).</p>
  
  <pre><code>apiVersion: extensions/v1beta1
  kind: Ingress
  metadata:
    name: hello-kubernetes-ingress
    annotations:
      kubernetes.io/ingress.class: nginx
      nginx.ingress.kubernetes.io/rewrite-target: /
  spec:
    rules:
    - host: first.testdomain.com
      http:
        paths:
        - backend:
            serviceName: hello-kubernetes-first
            servicePort: 80
          path: /foo
  </code></pre>
  
  <p>I've also tried <a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/" rel="nofollow noreferrer">this example</a> for paths / rewriting:</p>
  
  <pre><code>apiVersion: extensions/v1beta1
  kind: Ingress
  metadata:
    name: hello-kubernetes-ingress
    annotations:
      kubernetes.io/ingress.class: nginx
      nginx.ingress.kubernetes.io/rewrite-target: /$2
  spec:
    rules:
    - host: first.testdomain.com
      http:
        paths:
        - backend:
            serviceName: hello-kubernetes-first
            servicePort: 80
          path: /foo(/|$)(.*)
  </code></pre>
  
  <p>But the requests aren't even directed to the <code>hello-kubernetes-first</code> service.</p>
  
  <p>It appears that my rewrite configuration isn't making it to the <code>/etc/nginx/nginx.conf</code> file. When I run the following, I get no results:</p>
  
  <pre class="lang-sh prettyprint-override"><code>kubectl exec nginx-ingress-nginx-ingress-XXXXXXXXX-XXXXX  cat /etc/nginx/nginx.conf | grep rewrite
  </code></pre>
  
  <p>How do I get the path rewriting to work?</p>
  
  <p><strong>Additional information:</strong></p>
  
  <ul>
  <li>kubectl / kubernetes version: <code>v1.14.8</code></li>
  <li>Hosting on Azure Kubernetes Service (AKS)</li>
  </ul>