<p>I have two services one for serving static files and other for serving apis. I have created a single ingress controller for these.</p>
<p>I want to serve <code>/</code> from service1 and <code>/api</code> from service2. My services are running fine.
but I am getting 404 for <code>/api</code> path.</p>
<p>Below is my kubernetes yaml</p>
<pre><code>---
apiVersion: v1
kind: Namespace
metadata:
  name: &quot;myapp&quot;
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: &quot;service2&quot;
  namespace: &quot;myapp&quot;
  labels:
    project: &quot;myapp&quot;
    env: &quot;prod&quot;
spec:
  replicas: 2
  selector:
    matchLabels:
      project: &quot;myapp&quot;
      run: &quot;service2&quot;
    matchExpressions:
      - {key: project, operator: In, values: [&quot;myapp&quot;]}
  template:
    metadata:
      labels:
        project: &quot;myapp&quot;
        env: &quot;prod&quot;
        run: &quot;service2&quot;
    spec:
      securityContext:
        sysctls:
          - name: net.ipv4.ip_local_port_range
            value: &quot;1024 65535&quot;
      imagePullSecrets:
        - name: tildtr
      containers:
        - name: &quot;node-container&quot;
          image: &quot;images2&quot;
          imagePullPolicy: Always
          ports:
            - containerPort: 3000
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: &quot;service1&quot;
  namespace: &quot;myapp&quot;
  labels:
    project: &quot;myapp&quot;
    env: &quot;prod&quot;
spec:
  replicas: 2
  selector:
    matchLabels:
      project: &quot;myapp&quot;
      run: &quot;service1&quot;
    matchExpressions:
      - {key: project, operator: In, values: [&quot;myapp&quot;]}
  template:
    metadata:
      labels:
        project: &quot;myapp&quot;
        env: &quot;prod&quot;
        run: &quot;service1&quot;
    spec:
      securityContext:
        sysctls:
          - name: net.ipv4.ip_local_port_range
            value: &quot;1024 65535&quot;
      imagePullSecrets:
        - name: tildtr
      containers:
        - name: &quot;nginx-container&quot;
          image: &quot;image1&quot;
          imagePullPolicy: Always
          ports:
            - containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
  name: &quot;service1&quot;
  namespace: &quot;myapp&quot;
  labels:
    project: &quot;myapp&quot;
    env: &quot;prod&quot;
    run: &quot;service1&quot;
spec:
  selector:
    project: &quot;myapp&quot;
  type: ClusterIP
  ports:
    - name: &quot;service1&quot;
      port: 80
      targetPort: 80
  selector:
    run: &quot;service1&quot;
---
apiVersion: v1
kind: Service
metadata:
  name: &quot;service2&quot;
  namespace: &quot;myapp&quot;
  labels:
    project: &quot;myapp&quot;
    env: &quot;prod&quot;
    run: &quot;service2&quot;
spec:
  selector:
    project: &quot;myapp&quot;
  type: ClusterIP
  ports:
    - name: &quot;service2&quot;
      port: 80
      targetPort: 3000
  selector:
    run: &quot;service2&quot;
    
    
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: &quot;myapp&quot;
  namespace: &quot;myapp&quot;
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/server-alias: &quot;*.xyz.in&quot;
    nginx.ingress.kubernetes.io/server-snippet: |
      sendfile on;
      tcp_nopush on;
      tcp_nodelay on;
      keepalive_timeout 50;
      keepalive_requests 100000;
      reset_timedout_connection on;
      client_body_timeout 20;
      send_timeout 2;
      types_hash_max_size 2048;
      client_max_body_size 20M;
      gzip on;
      gzip_min_length 10240;
      gzip_proxied expired no-cache no-store private auth;
      gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml application/json;
      gzip_disable &quot;MSIE [1-6]\.&quot;;
spec:
  rules:
    - host: &quot;myhost.in&quot;
      http:
        paths:
          - path: /api
            backend:
              serviceName: &quot;service2&quot;
              servicePort: 80
          - path: /
            backend:
              serviceName: &quot;service1&quot;
              servicePort: 80


</code></pre>
<p>And this is my ingress desc.</p>
<pre><code>
Name:             myapp
Namespace:        myapp
Address:          10.100.160.106
Default backend:  default-http-backend:80 (&lt;error: endpoints &quot;default-http-backend&quot; not found&gt;)
Rules:
  Host                             Path  Backends
  ----                             ----  --------
  ds-vaccination.timesinternet.in  
                                   /api   service2:3000 (10.0.1.113:3000,10.0.2.123:3000)
                                   /      service1:80 (10.0.1.37:80,10.0.2.59:80)
Annotations:                       nginx.ingress.kubernetes.io/rewrite-target: /
                                   nginx.ingress.kubernetes.io/server-alias: *.xyz.in
                                   nginx.ingress.kubernetes.io/server-snippet:
                                     sendfile on;
                                     tcp_nopush on;
                                     tcp_nodelay on;
                                     keepalive_timeout 50;
                                     keepalive_requests 100000;
                                     reset_timedout_connection on;
                                     client_body_timeout 20;
                                     send_timeout 2;
                                     types_hash_max_size 2048;
                                     client_max_body_size 20M;
                                     gzip on;
                                     gzip_min_length 10240;
                                     gzip_proxied expired no-cache no-store private auth;
                                     gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml application/json;
                                     gzip_disable &quot;MSIE [1-6]\.&quot;;
Events:
  Type    Reason  Age               From                      Message
  ----    ------  ----              ----                      -------
  Normal  Sync    6s (x5 over 31m)  nginx-ingress-controller  Scheduled for sync
  Normal  Sync    6s (x5 over 31m)  nginx-ingress-controller  Scheduled for sync
</code></pre>

<p>remove this annotation and try</p>
<pre><code>annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
</code></pre>
<p>if your cluster is supporting old <strong>API</strong> : extensions/v1beta1</p>
<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: service-ingress
spec:
  rules:
  - host: service1.example.com
    http:
      paths:
      - backend:
          serviceName: service1
          servicePort: 80
  - host: service2.example.com
    http:
      paths:
      - backend:
          serviceName: service2
          servicePort: 80
</code></pre>
