简体   繁体   English

NGINX 使用 Slim API 框架从子目录重定向到根目录

[英]NGINX redirect from subdirectory to root with Slim API framework

I want to forward requests to the subdirectory v1 (subdomain.domain.com/v1) to my root in Nginx.我想将对子目录 v1 (subdomain.domain.com/v1) 的请求转发到我在 Nginx 中的根目录。 Tried this and this answer (and more variations) with no success.尝试了这个这个答案(以及更多变体)但没有成功。 I use the Slim API framework.我使用 Slim API 框架。

My nginx config looks like this:我的 nginx 配置如下所示:

    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;
    }
  }
}

My ingress yaml (Kubernetes) looks like this:我的入口 yaml (Kubernetes) 看起来像这样:

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: "false"
spec:
  rules:
    - host: NAME_OF_DOMAIN
      http:
        paths:
          - path: /v1/*
            backend:
              serviceName: svc-ENVIRONMENT_NAME_SHORT-NAME_OF_DEPLOYMENT
              servicePort: 443

I only get this thing to run without /v1 (just subdomain.domain.com).我只有在没有 /v1(只是 subdomain.domain.com)的情况下才能运行这个东西。 Any guidance appreciated.任何指导表示赞赏。

UPDATE (Jun 2021): in SLIM 4 this is now as easy as it gets: $app->setBasePath("/index.php/v1");更新(2021 年 6 月):在 SLIM 4 中,这变得非常简单:$app->setBasePath("/index.php/v1");

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 HTTP to HTTPS redirects yet, let alone any kind of redirect.但是,GCE 入口还不支持HTTP 到 HTTPS重定向,更不用说任何类型的重定向了。

The way you have it, is basically this way: the ingress gets a /v1 request then it goes to Nginx and then it says redirect to / but that redirect request goes back to the ingress which says I don't know what to do with / request.你拥有它的方式,基本上是这样的:入口收到一个/v1请求,然后它转到 Nginx 然后它说重定向到/但是重定向请求返回到入口,它说我不知道该怎么做/请求。

Another approach is just to have your backend serve on /v1 or use a two-layered approach which is pretty common.另一种方法是让您的后端服务于/v1或使用非常常见的两层方法。 GCE LB (with no ingress controller) ➡️ Nginx ingress (which does the redirect and has a much richer feature set) ➡️ backend. GCE LB(没有入口控制器)➡️ Nginx 入口(进行重定向并具有更丰富的功能集)➡️ 后端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM