<p>I want to forward requests to the subdirectory v1 (subdomain.domain.com/v1) to my root in Nginx. Tried <a href="https://stackoverflow.com/questions/42443468/nginx-location-configuration-subfolders">this</a> and <a href="https://stackoverflow.com/questions/22224441/nginx-redirect-all-requests-from-subdirectory-to-another-subdirectory-root">this</a> answer (and more variations) with no success. I use the Slim API framework.</p>
<p>My nginx config looks like this:</p>
<pre><code>    events {
}
http {
  include /etc/nginx/mime.types;
  server {
    listen 80;
    server_name subdomain.domain.com;
    root /var/www/html;
    index index.php;
    try_files $uri $uri/ /index.php?$query_string;
    location /healthcheck {
      auth_basic off;
      allow all;
      return 200;
    }
    location / {
      if (!-f $request_filename) {
        rewrite ^(.*)$ /index.php$1 last;
      }
    }
    #location ~ \.php {
    # only allow index.php to be proxied to FastCGI process (more secure than all php files!)
    location /index.php {
      proxy_pass http://127.0.0.1:9000;
    }
    location location ~ ^/v1/(.*) {
      return 301 $scheme://$http_host/$1$is_args$query_string;
    }
  }
}
</code></pre>
<p>My ingress yaml (Kubernetes) looks like this:</p>
<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
 name: ingress-ENVIRONMENT_NAME_SHORT-NAME_OF_DEPLOYMENT
 namespace: NAME_OF_NAMESPACE
 annotations:
   kubernetes.io/ingress.class: gce
   kubernetes.io/ingress.global-static-ip-name: ip-ENVIRONMENT_NAME_SHORT-NAME_OF_DEPLOYMENT
   networking.gke.io/managed-certificates: cert-ENVIRONMENT_NAME_SHORT-NAME_OF_DEPLOYMENT
   kubernetes.io/ingress.allow-http: &quot;false&quot;
spec:
  rules:
    - host: NAME_OF_DOMAIN
      http:
        paths:
          - path: /v1/*
            backend:
              serviceName: svc-ENVIRONMENT_NAME_SHORT-NAME_OF_DEPLOYMENT
              servicePort: 443
</code></pre>
<p>I only get this thing to run without /v1 (just subdomain.domain.com). Any guidance appreciated.</p>
<p>UPDATE (Jun 2021): in SLIM 4 this is now as easy as it gets:
$app-&gt;setBasePath(&quot;/index.php/v1&quot;);</p>

chriscross
<p>The best approach I believe is to have the redirect happen at the ingress level or even prior to hitting the ingress. However, GCE ingress doesn't support <a href="https://github.com/kubernetes/ingress-gce/issues/1075" rel="nofollow noreferrer">HTTP to HTTPS</a> redirects yet, let alone any kind of redirect.</p>
<p>The way you have it, is basically this way: the ingress gets a <code>/v1</code> request then it goes to Nginx and then it says redirect to <code>/</code> but that redirect request goes back to the ingress which says I don't know what to do with <code>/</code> request.</p>
<p>Another approach is just to have your backend serve on <code>/v1</code> or use a two-layered approach which is pretty common.  GCE LB (with no ingress controller) ➡️  Nginx ingress (which does the redirect and has a much richer feature set) ➡️ backend.</p>
