【问题标题】:Kubernetes Ingress NGINX for Laravel via fast-cgiKubernetes Ingress NGINX for Laravel 通过 fast-cgi
【发布时间】:2021-03-04 08:00:57
【问题描述】:

我在我的 Kubernetes 集群中使用 Ingress NGINX:https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/

由于 Ingress NGINX 支持 Fast CGI 作为直接后端,我在端口 9000 上运行了一个 Laravel 应用程序通过 php-fpm 服务的容器。现在我尝试使用 Ingress NGINX 作为代理,FCGI 作为后端,但我无法配置它。

根据 Laravel 文档,一个 nginx 配置应该如下所示(https://laravel.com/docs/8.x/deployment):

server {
    listen 80;
    server_name example.com;
    root /srv/example.com/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

不幸的是,似乎无法为/~ \.php$ 创建位置,因为 Ingress NGINX 确实无缘无故地保留了位置 /

我的配置如下:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-fastcgi
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
    kubernetes.io/ingress.allow-http: "false"
    cert-manager.io/cluster-issuer: "letsencrypt-production"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/backend-protocol: "FCGI"
    nginx.ingress.kubernetes.io/fastcgi-index: "index.php"
spec:
  tls:
    - hosts:
        - api.abcxyz.cloud
      secretName: api-cert-secret
  rules:
    - host: api.abcxyz.cloud
      http:
        paths:
          - path: /
            backend:
              serviceName: laravel
              servicePort: 9000

如何使用 Ingress NGINX 和 FCGI 处理来自 Laravel 的需求?

【问题讨论】:

    标签: php nginx kubernetes kubernetes-ingress nginx-ingress


    【解决方案1】:

    我觉得你的nginx配置的监听端口应该是9000。

    我猜,你拥有的是:

    • 一个带有 PHP 应用程序的容器,由 Nginx 在端口 9000 提供服务。
    • 服务端口 80 上的 ClusterIP 服务,映射到您的端口 9000 容器端口。
    • 那么您正在尝试使用 ingress 向外部世界公开您的服务

    所以你需要以下 k8 声明

    • Deployment/Statefulset - 创建您的容器。
    • Service - 将您的服务端口 80 映射到容器端口 9000
    • Ingress - 在端口 80 上将您的服务公开给外部工作

    这里是一个例子(虽然我用ambassadoringress.class 做minikube但是原理是一样的)https://github.com/francisfueconcillo/k8-geektalk/blob/master/wp-deployment.yaml

    【讨论】:

      【解决方案2】:

      不,您不能将服务映射到端口 9000,因为端口 9000 位于 php-fpm fast cgi 中,它不是 Web 服务。为了让它工作,你需要设置script_filename,让kubernetes通过php-fpm了解运行哪个脚本。 参考:https://kubernetes.github.io/ingress-nginx/user-guide/fcgi-services/

      如果您只需要运行一个您需要的后端。 但是,如果您尝试映射多个路径,一个使用 php-fpm,另一个使用普通 http 模式,则不支持。因为在 fastcgi 模式下的 kubernetes ingress 将运行它的所有路径。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-26
        • 2013-05-08
        • 2021-05-15
        • 1970-01-01
        • 2019-02-24
        • 2018-07-05
        • 2019-08-01
        • 2022-12-10
        相关资源
        最近更新 更多