Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to setup ingress to serve static content on kubernetes? #323

Closed
kevin-jj opened this issue Jul 25, 2018 · 7 comments
Closed

How to setup ingress to serve static content on kubernetes? #323

kevin-jj opened this issue Jul 25, 2018 · 7 comments

Comments

@kevin-jj
Copy link

kevin-jj commented Jul 25, 2018

How to setup ingress to serve static content at my app? The following annotation does not work. Since the Nginx ingress runs on a separate deployment with app pod, it seems like it cloud not access static content in app pod.
@pleshakov

annotations:
kubernetes.io/ingress.class: "nginx"
nginx.org/client-max-body-size: "2m"
nginx.org/server-snippets: |
location ~ ^/(assets)/{
root /var/www/test/public;
gzip_static on;
expires max;
add_header Cache-Control public;
}

@Dean-Coakley Dean-Coakley added the question An issue asking a question label Jul 25, 2018
@kevin-jj kevin-jj changed the title How to setup ingress to serve static content? How to setup ingress to serve static content on kubernetes? Jul 26, 2018
@isaachawley
Copy link
Contributor

The Ingress Controller is not designed to serve static content. If you'd like to serve static content, consider having a separate Service for it.

See #257 (comment)

@Dean-Coakley Dean-Coakley added invalid and removed question An issue asking a question labels Aug 9, 2018
@demidovsky
Copy link

demidovsky commented Jan 11, 2019

I know that this is an old issue, but for some cases the answer on "How to setup ingress to serve static content on kubernetes?" may be just setting add-base-url: "true" and rewrite-target: / annotations,

e.g.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress-nginx
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/add-base-url: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: some.kind.of.hostname.co
    http: &rules
      paths:
      - path: /path/to
        backend:
          serviceName: some-website-serving-static
          servicePort: 80
      - path: /
        backend:
          serviceName: another-service
          servicePort: 80

Hope this helps someone.

@blind3dd
Copy link

blind3dd commented Feb 8, 2019

it works fine indeed, but be careful with rewrites :)

@majidshaikh16
Copy link

majidshaikh16 commented Jun 1, 2020

It

I know that this is an old issue, but for some cases the answer on "How to setup ingress to serve static content on kubernetes?" may be just setting add-base-url: "true" and rewrite-target: / annotations,

e.g.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress-nginx
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/add-base-url: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: some.kind.of.hostname.co
    http: &rules
      paths:
      - path: /path/to
        backend:
          serviceName: some-website-serving-static
          servicePort: 80
      - path: /
        backend:
          serviceName: another-service
          servicePort: 80

Hope this helps someone.

Hi @demidovsky,
I did similarly but still, it's not serving the js or CSS files I am using angular as my frontend
My Ingress file:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-service
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/add-base-url: "true"
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /app
backend:
serviceName: food-app-cluster-ip-services
servicePort: 80

Issue:
image

@wriver1
Copy link

wriver1 commented Jun 16, 2020

I have the same problem

@majidshaikh16
Copy link

I have solved the issue by setting the base URL for all the static content to eg: /app/style.js from style.js and then it worked fine for me.

@oscarlofwenhamn
Copy link

Be advised that the nginx.ingress.kubernetes.io annotations relate to the kubernetes/ingress-nginx and not this one. You can find all annotations for the nginxinc/kubernetes-ingress via the official docs.

You can see this because pretty much all nginxinc/kubernetes-ingress annotations are prefixed by nginx.org or nginx.com, and the kubernetes/ingress-nginx annotations by nginx.ingress.kubernetes.io (by default). Hopefully this can help some who are troubleshooting and looking for the correct docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants