Ingress nginx loading resource 404 in kubernetes

0 votes

I'm trying to get my website up and running on kubernetes. I'm using ingress to route the site.

nginx-conf:

server {
  listen 80;
  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    try_files $uri $uri/ /index.html =404;
  }
}

Kubernetes deployment:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: mywebsite
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: mywebsite
    spec:
      containers:
      - name: mywebsite
        image: containerurl/xxx/mywebsite:latest
        ports:
        - containerPort: 80

---
apiVersion: v1
kind: Service
metadata:
  name: mywebsite
spec:
  ports:
  - port: 82
    targetPort: 80

  selector:
    app: mywebsite

Ingress config:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myIngress
  annotations:
    kubernetes.io/tls-acme: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  tls:
  - hosts:
    - xxx.com
    secretName: tls-secret
  rules:
  - host: xxx.com
    http:
      paths:
      - path: /mywebsite
        backend:
          serviceName: mywebsite
          servicePort: 82

When i go to xxx.com/mywebiste, the index.html is loading, but the css and js are not loaded (404):

index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>My Website</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <link href="/styles.30457fcf45ea2da7cf6a.css" rel="stylesheet"></head>
  <body>
  <div id="react-root"></div>
  <script type="text/javascript" src="/app.171d6a95cf24d8abab14.js"></script></body>
</html>

Any idea on how can this work?

Sep 10, 2018 in Kubernetes by lina
• 8,220 points
8,139 views

1 answer to this question.

0 votes

This is not a routing problem on nginx part, but the browser trying to access an absolute URI from the root of your domain. Use relative URIs (remove the leading slash):

<link href="styles.30457fcf45ea2da7cf6a.css" rel="stylesheet"></head>
<script type="text/javascript" src="app.171d6a95cf24d8abab14.js"></script></body>
answered Sep 10, 2018 by Kalgi
• 52,350 points

I am facing same issue.

I am using mongo-express to visualize my mongo db data. But when this is exposed using ingress, the web page gives 404 results for loading images/css/js..

Any thoughts ?

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: mongo-express
  namespace: {{ .Values.namespace }}
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
  - http:
      paths:
      - path: /baster/mongo-express
        backend:
          serviceName: {{ .Values.mongo_express.service_name }}
          servicePort: 8081

Related Questions In Kubernetes

+1 vote
1 answer

Is autoscaling possible in kubernetes ingress nginx

In principle, the NGINX ingress controller is ...READ MORE

answered Oct 24, 2018 in Kubernetes by Kalgi
• 52,350 points
1,581 views
0 votes
2 answers

DNS does not resolve with NGINX in Kubernetes

adding to @kalgi's answer Using just the hostname ...READ MORE

answered Aug 30, 2018 in Kubernetes by Nilesh
• 7,060 points
5,608 views
0 votes
1 answer

Filter source ip in kubernetes ingress in GCE

This feature currently works only with nginx. Example ...READ MORE

answered Sep 7, 2018 in Kubernetes by Kalgi
• 52,350 points
1,258 views
0 votes
3 answers

Nginx reverse proxy URL getting rewritten

Hey @Gopi, try your ingress probably like ...READ MORE

answered Feb 12, 2019 in Kubernetes by Kalgi
• 52,350 points
4,711 views
0 votes
1 answer

Kubernetes nginx-ingress TLS issue

You have to create a secret named test-secret. ➜ ...READ MORE

answered Sep 11, 2018 in Kubernetes by Kalgi
• 52,350 points
1,969 views
0 votes
3 answers

Error while joining cluster with node

Hi Kalgi after following above steps it ...READ MORE

answered Jan 17, 2019 in Others by anonymous
15,455 views
+1 vote
1 answer

kubernetes ingress controller and resource using nginx

Ingress is just collection of rules that forwards ...READ MORE

answered Sep 12, 2018 in Kubernetes by Kalgi
• 52,350 points
1,769 views
+1 vote
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP