NGINX-PHP-FPM多个应用程序K8s /入口

时间:2018-08-27 13:58:11

标签: php symfony nginx kubernetes

我有一个带有Ingress的k8s应用程序,我想使用它使用子域为其他应用程序提供服务。

这是我的完整配置:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: symfony-nginx
  namespace: default
  labels:
    app: symfony-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: symfony-nginx
  template:
    metadata:
      labels:
        app: symfony-nginx
    spec:
      volumes:
        - name: symfony-nginx
          configMap:
            name: symfony-nginx-configmap
      containers:
      - name: symfony-nginx
        image: nginx:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /etc/nginx/conf.d
          name: symfony-nginx
        resources:
          requests:
            memory: 32Mi
            cpu: 10m
          limits:
            memory: 64Mi
            cpu: 30m
        readinessProbe:
          httpGet:
            path: /nginx-health
            port: 80
          initialDelaySeconds: 15
          timeoutSeconds: 1
          periodSeconds: 10
        livenessProbe:
          httpGet:
            path: /nginx-health
            port: 80
          initialDelaySeconds: 15
          timeoutSeconds: 1
          periodSeconds: 10
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: symfony-nginx-configmap
  namespace: default
data:
  default.conf: |
      server {
          server_name php-docker.local;
          error_log  /var/log/nginx/error.log;
          access_log /var/log/nginx/access.log;
          root /var/www/html/symfony/public;

          proxy_buffering off;

          location = /nginx-health {
              access_log off;
              return 200 "healthy\n";
          }

          location / {
              try_files $uri /index.php$is_args$args;
          }

          location ~ \.php {
              fastcgi_split_path_info ^(.+\.php)(/.+)$;
              fastcgi_pass symfony:9000;
              include fastcgi_params;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              fastcgi_param DOCUMENT_ROOT $document_root;
              internal;
          }
          location ~ \.php$ {
                  return 404;
          }
      }
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: symfony-nginx-hpa
  namespace: default
  labels:
    app: symfony-nginx
spec:
  scaleTargetRef:
    kind: Deployment
    name: symfony-nginx
    apiVersion: apps/v1
  minReplicas: 1
  maxReplicas: 10
  targetCPUUtilizationPercentage: 70
---
apiVersion: v1
kind: Service
metadata:
  name: symfony-nginx-np
  namespace: default
  labels:
    app: symfony-nginx
spec:
  ports:
  - protocol: TCP
    port: 80
  selector:
    app: symfony-nginx
  type: NodePort
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: symfony-nginx-ingress
spec:
  rules:
  - http:
      paths:
      - path: /symfony
        backend:
          serviceName: symfony-nginx-np
          servicePort: 80

我只收到'找不到“ GET / symfony”的路由” ,因为到达php-fpm的uri保留了'symfony'前缀。

我该如何责怪NGINX在'symfony'之后仅将uri的一部分发送到php-fpm?

[更新]

也许这是一个symfony应用程序问题,因为我在http://mydomanin.com/symfony上收到了index.php的响应,并带有'找不到“ GET / symfony”的路由” ?还是这是NGINX的责任?

谢谢,问候

2 个答案:

答案 0 :(得分:0)

最好的解决方案是为交响乐本身中的所有路由设置一个/symphony前缀。

另一种方法是在nginx上使用rewrite

rewrite ^/symphony/(.*)$ /$1 last;

但是要当心后果! Symphony将返回带有'/myImage.jpg'的图像URL,与js / css / ...相同。链接将为”,依此类推,这些页面将不会映射到您的应用程序。

答案 1 :(得分:0)

最后我找到了解决方案。

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: symfony-nginx-ingress
spec:
  rules:
  - http:
      paths:
      - path: /symfony
        backend:
          serviceName: symfony-nginx-np
          servicePort: 80

此Ingress配置仅与/ symfony路由匹配,因此要完成此配置,还需要添加/ symfony / * ...在我看来,这不是很优雅...