Routing ingress controller traffic based upon file type of path

7/8/2019

Is it possible to route ingress controller traffic to different services/deployments based upon the file type in the path? For example if the path was:

domain.com/directory/hello.html -> (Frontend Service)
domain.com/directory/hello.php -> (Backend Service)

The architecture I have designed looks like this: enter image description here

Does this look suitable and is this possible, or is there a better way of achieving this?

My ingress controller looks like:

kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  name: vote-ingress
  namespace: default
  selfLink: /apis/extensions/v1beta1/namespaces/default/ingresses/vote-ingress
  uid: 597158e6-a0ce-11e9-b3b1-00155d599803
  resourceVersion: '268064'
  generation: 1
  creationTimestamp: '2019-07-07T15:46:13Z'
spec:
  rules:
    - host: localhost
      http:
        paths:
          - path: /*.php
            backend:
              serviceName: website-backend
              servicePort: 80
          - path: /
            backend:
              serviceName: website-frontend
              servicePort: 80
status:
  loadBalancer:
    ingress:
      - hostname: localhost
-- Matt Davis
kubernetes
kubernetes-ingress
nginx
nginx-ingress

2 Answers

1/16/2020

You can route traffic to different service by file extension (by Regex). I have working Helm chart with same configuration. Partial example:

kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
    http:
      paths:
      - path: '/(.*)'
        backend:
          serviceName: website-backend
          servicePort: 80
      - path: '/(.+\.(jpg|svg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm|ttf|woff|woff2))'
        backend:
          serviceName: website-static
          servicePort: 80
-- daltin
Source: StackOverflow

7/16/2019

Is it possible to route ingress controller traffic to different services/deployments based upon the file type in the path?

No in that way. You would need to route your request to NGINX which will send request for *.php to a PHP_FPM. You can have that as separate Containers inside one Pod or as Services. Service example is nicely explained in How To Deploy a PHP Application with Kubernetes on Ubuntu 16.04

In this example you will have two containers NIGNX and PHP_FPM running on the Pod. PHP-FPM will handle dynamic PHP processing, and the NGINX will act as a web server.

You will need to use custom nginx.conf configuration and in my opinion the easiest way would be using ConfigMap.

Your ConfigMap might look like the following:

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  config : |
    server {
      index index.php index.html;
      error_log  /var/log/nginx/error.log;
      access_log /var/log/nginx/access.log;
      root /;

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

      location ~ \.php$ {
          try_files $uri =404;
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass 127.0.0.1:9000;
          fastcgi_index index.php;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }

Nginx will catch and send all *.php request via localhost:9000 to PHP-FPM container.

You can include the ConfigMap in your POD using the following:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
    - name: nginx-container
      image: nginx:1.7.9
      volumeMounts:
      - name: config-volume
        mountPath: /etc/nginx/nginx.conf
        subPath: nginx.conf
  volumes:
    - name: config-volume
      configMap:
        name: nginx-config
  restartPolicy: Never
-- Crou
Source: StackOverflow