Kubernetes - Nginx ingress controller - GCS - Redirect to `index.html` if no path in URL

Viewed 2551

I have this ingress and service created on my Kubernetes cluster

apiVersion: v1
metadata:
  name: google-storage-buckets
spec:
  type: ExternalName
  externalName: storage.googleapis.com
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: proxy-assets-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /kinto-static-websites/gatsby/public/$1
    nginx.ingress.kubernetes.io/upstream-vhost: "storage.googleapis.com"
    nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
spec:
  rules:
  - host: gatsby.vegeta.kintohub.net
    http:
      paths:
      - path: /(.*)$
        backend:
          serviceName: google-storage-buckets
          servicePort: 443

However, this works only if I add index.html after gatsby.vegeta.kintohub.net.

Same if I go on gatsby.vegeta.kintohub.net/page-2.

How could I make this work plz?

Thanks

2 Answers

Here it's more about your nginx.conf and what it thinks it's a default file to try .

The default ingress configuration doesn't have such as thing as always try index.html, so you need to add some extra configuration to your Ingress Kubernetes resource. In the case of the nginx ingress controller, you can use the configuration-snippet annotation which will add a config under the location directive. Something like this should do:

nginx.ingress.kubernetes.io/configuration-snippet: |
      try_files $uri $uri/ $uri/index.html /index.html; 

✌️

We had a very similar case, gatsby static site on a GCP bucket.

we also tested try_files and index directives but didn't work.

In our case these hacky configuration-snippets did the trick:

kind: Service
apiVersion: v1
metadata:
  name: gcp-storage-bucket
spec:
  type: ExternalName
  externalName: storage.googleapis.com

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /<BUCKET_NAME>$uri
    nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
    nginx.ingress.kubernetes.io/upstream-vhost: "storage.googleapis.com"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      if ($uri ~ "^/(.*)/$") {
        rewrite ^(.+)/$ $1 last;
        proxy_pass  https://storage.googleapis.com;
      }
      if ($uri ~ "^\/$") {
        rewrite ^ /<BUCKET_NAME>/index.html break;
        proxy_pass  https://storage.googleapis.com;
      }
      if ($uri !~ "^(.*)\.(.*)$") {
        rewrite ^ /<BUCKET_NAME>$uri/index.html break;
        proxy_pass  https://storage.googleapis.com;
      }
  labels:
    app.kubernetes.io/instance: static-site.example.com
  name: static-site.example.com
  namespace: default
spec:
  rules:
  - host: static-site.example.com
    http:
      paths:
      - path: /(.*)
        backend:
          service:
            name: gcp-storage-bucket
            port:
              number: 443
        pathType: Prefix

Everything seems to be working fine on our case, except for the 404s

There might be a more efficient way to do this but hopes this helps.

Related