Kubernetes Ingress Nginx加载资源404 [英] Kubernetes Ingress Nginx loading resources 404

查看:167
本文介绍了Kubernetes Ingress Nginx加载资源404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试使我们的网站在kubernetes上运行(使用nginx在容器中运行).我们使用入口来路由到站点,这是我们的配置:

We're trying to get our website working on kubernetes (running in a container using nginx). We use ingress to route to the site, here is our configuration:

nginx-conf:

nginx-conf:

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

Kubernetes部署:

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

入口配置:

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

当我转到xxx.com/mywebiste时,index.html正在加载,但是css和js未加载(404):

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

index.html:

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>

...因为它尝试在此处获取资源,例如css的示例:

...because it trys to get the resources here, as example of the css:

xxx.com/styles.30457fcf45ea2da7cf6a.css

...而不是:

xxx.com/mywebsite/styles.30457fcf45ea2da7cf6a.css

xxx.com/mywebsite/styles.30457fcf45ea2da7cf6a.css

如果尝试了不同的操作,例如:

If tried different things like:

nginx.ingress.kubernetes.io/add-base-url
nginx.ingress.kubernetes.io/app-root

...等等.,但似乎没有任何作用.

...etc., but nothing seems to work.

有什么想法吗?感谢您的帮助.

Any ideas? Thanks for your help.

关于, 彼得

推荐答案

这不是nginx部分的路由问题,而是浏览器尝试从您的域的根目录访问绝对URI.使用相对URI(删除斜杠):

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>

这篇关于Kubernetes Ingress Nginx加载资源404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆