NGINX - PHP-FPM multiple application K8s/Ingress

8/27/2018

I have a k8s application with an Ingress that I would like to use to serve differents applications using a subdomain.

This is my full configuration :

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

And I only receive 'No route found for "GET /symfony"' because the uri that arrive to php-fpm mantain the 'symfony' prefix.

How can I chage the NGINX to send to php-fpm only the part of uri after the 'symfony' ??

[UPDATE]

Maybe it is a symfony application problem, because I get the response of the index.php on http://mydomanin.com/symfony with a 'No route found for "GET /symfony"' ? Or this is a NGINX responsability?

Thank you, regards

-- Alessandro Candon
kubernetes
nginx
php
symfony

2 Answers

8/28/2018

The best solution would be to set a /symphony prefix for all route in symphony itself.

Another way would be to use rewrite on nginx:

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

But then be ware of the consequences! Symphony will return image url with '/myImage.jpg', same for js/css/... Link will be '' and so on, those page will not be mapped to your application.

-- wargre
Source: StackOverflow

9/9/2018

At the end I found a solution.

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

this Ingress configuration only match the /symfony route, so to complete it you need to also add /symfony/* ... That isn't very elegant in my opinion...

-- Alessandro Candon
Source: StackOverflow